Yes, it's possible - if the second function passed to the .then
throws an error (or returns a rejected Promise
), the error will be passed down to the next .catch
:
Promise.resolve()
.then(function(data) {
//some logic
throw new Error();
})
.then((data) => {
// more logic
}, function(err) {
// first error block
console.log('Handling first error');
return err.somePropertyThatDoesNotExist.text;
})
.catch(function(err) {
// second error block
console.log('Handling second error')
});
As comment notes, the catch
will also run if the first function passed to the second .then
throws an error:
Promise.resolve()
.then(function(data) {
//some logic
return 'Foo';
})
.then((data) => {
// more logic
throw new Error();
}, function(err) {
// first error block
console.log('Handling error in then');
})
.catch(function(err) {
// second error block
console.log('Handling error in catch')
});