Is it possible to ignore a catch and return back to the chain?
promiseA() // <-- fails with 'missing' reason
.then(promiseB) // <-- these are not going to run
.then(promiseC)
.catch(function(error, ignore){
if(error.type == 'missing'){
ignore() // <-- ignore the catch and run promiseB and promiseC
}
})
Is something like this possible?
This function has one argument: reason. The rejection reason. The Promise returned by catch() is rejected if onRejected throws an error or returns a Promise which is itself rejected; otherwise, it is fulfilled.
The code of a promise executor and promise handlers has an "invisible try.. catch " around it. If an exception happens, it gets caught and treated as a rejection.
then() The then() method returns a Promise . It takes up to two arguments: callback functions for the success and failure cases of the Promise .
all() method returns an array as an output containing promise data inside several indexes. Promise. allSettled() method returns an array of objects and each of these objects further contains two properties further status and value.
Here's the synchronous analogy:
try {
action1(); // throws
action2(); // skipped
action3(); // skipped
} catch (e) {
// can't resume
}
vs
try {
action1(); // throws
} catch (e) {
handleError(e);
}
action2(); // executes normally
action3();
Here's the promise version:
asyncActionA() // <-- fails with 'missing' reason
.catch(error => {
if(error.type == 'missing'){
return; // Makes sure the promise is resolved, so the chain continues
}
throw error; // Otherwise, rethrow to keep the Promise rejected
})
.asyncActionB(promiseB) // <-- runs
.asyncActionC(promiseC)
.catch(err => {
// Handle errors which are not of type 'missing'.
});
If you need just ignore all error in promiseA, you can just do it like that:
promiseA()
.catch(function(error){
//just do nothing, returns promise resolved with undefined
})
.then(promiseB)
.then(promiseC)
If you need to run promiseB only when error.type == 'missing'
, you can do that:
promiseA()
.catch(function(error, ignore){
if(error.type == 'missing'){
return promiseB.then(promiseC)
}
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With