为什么可以在最后只使用一个 catch()?

问题描述 投票:0回答:0

我知道

promise 3
被拒绝是因为它解决了一个 已经被拒绝的承诺 (
promise 2
),但是
promise 3
怎么会拒绝
promise 4
?, 幕后发生了什么?

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);
 });

输出:

then 1:6
index.js:57 Error: ups - rejected
    at index.js:46:27
javascript exception asynchronous error-handling promise
© www.soinside.com 2019 - 2024. All rights reserved.