I understand that promise 3
is rejected because it resolves to an already rejected promise (promise 2
), but how does promise 3
get to reject promise 4
?, what is going on behind the scenes?
Promise.resolve(6) // promise 1
.then(function(data) {
console.log('then 1:' + data);
return Promise.reject(new Error('ups - rejected')); // Does it "jump" to the first catch() it finds?
}) // promise 2 - this promise is resolved with a **rejected promise**
.then(function (data) {
console.log('then 2:' + data);
return data + 1;
}) // promise 3
.then(function (data) {
console.log('then 3:' + data);
return data + 1;
}) // promise 4
.catch(function(error) { // catch of promise 4?
console.log(error);
});
Output:
then 1:6
index.js:57 Error: ups - rejected
at index.js:46:27