我一直很好奇,如果承诺在任何位置遭到拒绝,以下是否then()还会被执行?以下面的代码为例:
Promise.reject('reJECTed')
.then(() => {
console.log('before resolve()');
return Promise.resolve('reSOLVed');
})
.then((msg) => {
console.log('reSOLVed inside 1st then()');
console.log(msg);
}, (msg) => {
console.log('reJECTed inside 1st then()');
console.log(msg);
})
.then((msg) => {
console.log('reSOLVing inside 2nd then()');
console.log(msg);
}, (msg) => {
console.log('reJECTing inside 2nd then()');
console.log(msg);
})
.catch((msg) => {
console.log('reJECTed in catch()');
console.log(msg);
});
Run Code Online (Sandbox Code Playgroud)
它会打印出来
reJECTed inside 1st then()
reJECTed
reSOLVing inside 2nd then()
undefined
Run Code Online (Sandbox Code Playgroud)
在控制台上,这意味着resolve()第二个then()和最后一个catch()没有被执行.是否遇到了当的意思是reject(),任何下列resolve()内A S then() …
我发现了一个关于 Promise 的有趣的事情。当我运行以下代码时,它给了我与 'bb' 相反的 'aa' 输出,这让我很困惑。有谁明白原因并解释一下吗?谢谢!
Promise.resolve('aa')
.then(Promise.resolve('bb'))
.then(console.log);Run Code Online (Sandbox Code Playgroud)