我想将3个承诺链接在一起但是有些行为我无法弄清楚它resolve和reject功能.为了问这个问题,我简化了我的代码:
function testP(num) {
return new Promise((resolve, reject) => {
console.log('Do Work', num);
reject(num);
});
}
function testPromises() {
return new Promise((resolve, reject) => {
testP(1)
.then(testP(2))
.then(testP(3))
.then(resolve)
.catch(reject);
});
};
const theTest = testPromises().then(()=>{
console.log("all done");
}).catch(err => {
console.log("ERR", err);
});
Run Code Online (Sandbox Code Playgroud)
我在输出中看到的是:
Do Work 1
Do Work 2
Do Work 3
ERR 1
Run Code Online (Sandbox Code Playgroud)
为什么代码会到达Do Work 2,Do Work 3如果第一个承诺reject立即命中?我的理解是then函数等待执行时resolve或reject之前的承诺.