2 return new Promise((resolve, reject) => {
3 const timeout = 2000;
4 setTimeout(() => {
5 reject(false); 6 }, timeout);
7
8 const intv = setInterval(async () => {
It is considered good practice to only pass instances of the built-in Error
object to the reject()
function for user-defined errors in Promises
. Error
objects automatically store a stack trace, which can be used to debug an error by determining where it came from. If a Promise
is rejected with a non-Error
value, it can be difficult to determine where the rejection occurred.
It helps us to ensure that Promises are rejected with Error
objects.
Promise.reject("something bad happened");
Promise.reject(5);
Promise.reject();
new Promise(function(resolve, reject) {
reject("something bad happened");
});
new Promise(function(resolve, reject) {
reject();
});
Promise.reject(new Error("something bad happened"));
Promise.reject(new TypeError("something bad happened"));
new Promise(function(resolve, reject) {
reject(new Error("something bad happened"));
});
var foo = getUnknownValue();
Promise.reject(foo);