I am currently wondering why the throw in this ES6 native Promise setup is not arriving in the catch block
new Promise(function(resolve,reject){
reject('bar')
}).then(function resolved(){
console.log('resolved 1');
}, function rejected(){
console.log('rejected 1')
throw new Error();
}).then(function resolved(val){
console.log('resolved 2');
}, function rejected(){
console.log('rejected 2');
}).catch(function(err){
console.log('catch');
});
I am looking for a way to get the control flow to the catch block, but if I use a rejected handler, if I throw an error, the control ends up there, and not in the catch.
In simpler terms, I am looking for a way to end up in the catch block, even if there is a onRejected handler...is there a way to do that?
new Promise(function(resolve,reject){
throw new Error(); // this goes to onRejected
reject('bar'); // this goes to onRejected
}).then(function onResolved(){
console.log('resolved');
}, function onRejected(){
console.log('rejected')
}).catch(function(err){
console.log('catch');
});
my goal is to branch separately, based on whether an error is thrown versus whether reject is called. Not sure if it's possible. Perhaps there is a way to call catch explicitly? I'd like to find a way to do this without explicitly throwing a new error in the final onRejected handler if possible.
Here is my goal, with comments:
new Promise(function(resolve,reject){
if(success){
resolve('success'); //this goes to next onResolved
}
else if(fail){
reject('fail'); //this goes to next onRejected (or catch if there is no onRejected)
}
else {
throw new Error('Fatal'); //this goes to next catch
}
});
that's the behavior I am looking for