With the following code, when I reject the promise, I see the first console.log but I also see the second console.log. This is expected since the rejection only affects the next "then()".
The question is, is there any operator equivalent to a "break" in a loop, so that you can jump off the chain? The fast answer would be yes, with a non-catched reject() or throw(), but both approaches will send an error message to the console (will they stop the whole code execution?)
So the question is, can it be done "cleanly"?
*My first assumption was that by catching the reject/error this would do the trick, but it is not the case
Promise.resolve()
.then(function(){
do stuff
return Promise.resolve();
})
.then(function(){
if (a === 'this')
return Promise.resolve();
else
// is there any "break" equivalent here?
return Promise.reject();
})
.then(function(){
console.log('seen if not rejected');
},function(){
console.log('seen if rejected');
})
.then(function(){
console.log('seen all the time');
});
Both .then(success, error)
callbacks return promises.
That is:
Promise.reject()
.then(function () {
// won't be called, as above promise is rejected
}, function () {
// will be called, and returns a resolved promise by default;
// if you want the next error callback to be called,
// you need to return a rejected promise or to throw an error
})
.then(function () {
// 1: called if above .then returns a resolved promise
}, function () {
// 2: called if above .then returns a rejected promise or throws an error
})