It is more common to pass to the reject
function not a string with an error, but an
object with an error:
let promise = new Promise(function(resolve, reject) {
setTimeout(function() {
reject(new Error('error in promise')); // an object with an error
}, 3000);
});
You can also throw objects with errors
using throw
- this will be
equivalent to passing them to
reject
:
let promise = new Promise(function(resolve, reject) {
setTimeout(function() {
throw new Error('error in promise'); // the equivalent of reject
}, 3000);
});
Modify the following code according to what you learned:
let promise = new Promise(function(resolve, reject) {
setTimeout(function() {
let isError = false;
if (!isError) {
resolve('success');
} else {
reject('error');
}
}, 3000);
});