javascript – 将拒绝()跳过Promise中的所有关注then()
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 将拒绝()跳过Promise中的所有关注then(),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3065字,纯文字阅读大概需要5分钟。
内容图文
我一直很好奇,如果Promise在任何位置都被拒绝,那么下面的()仍然会被执行吗?以下面的代码为例:
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);
});
它会打印出来
reJECTed inside 1st then()
reJECTed
reSOLVing inside 2nd then()
undefined
在控制台上,这意味着第二个then()中的resolve()和最后一个catch()没有被执行.这是否意味着当遇到reject()时,then()中的任何后续resolve()被完全跳过,直到捕获被拒绝?
谢谢你的解释!
解决方法:
Does that mean when met reject(), any following resolve()s inside a then() is totally skipped until the rejection is caught?
是.当promise拒绝时,链中的所有解析处理程序都会被跳过,直到某个拒绝处理程序处理拒绝并将promise链更改回来实现.
并且,如果您没有意识到,.then()处理程序的第二个参数是拒绝处理程序(与.catch()处理程序几乎相同).
但是,请记住,如果您有一个拒绝处理程序,并且它没有抛出或返回被拒绝的承诺,那么由于您已“处理”拒绝,该链将再次得到满足.它与try / catch完全一样.如果捕获并且不重新抛出,则处理异常并在此之后继续正常执行.
因此,在您的拒绝处理程序中,在1st then()中输出reJECTed,您不会返回任何内容,因此在此时,promise链将变为满足.此时拒绝已被“处理??”,承诺链现在切换到履行状态.
这是你的代码的一些注释:
Promise.reject('reJECTed')
.then(() => {
// this fulfilled handler is skipped because the promise chain is rejected here
console.log('before resolve()');
return Promise.resolve('reSOLVed');
})
.then((msg) => {
// this fulfilled handler is skipped because the promise chain is rejected here
console.log('reSOLVed inside 1st then()');
console.log(msg);
}, (msg) => {
// this reject handler is called because the promise chain is rejected here
console.log('reJECTed inside 1st then()');
console.log(msg);
// because this does not rethrow or return a rejected promise
// the promise chain switches to fulfilled
})
.then((msg) => {
// this fulfilled handler is called because the promise chain is fulfilled now
console.log('reSOLVing inside 2nd then()');
console.log(msg);
}, (msg) => {
// this reject handler is not called because the promise chain is fulfilled now
console.log('reJECTing inside 2nd then()');
console.log(msg);
})
.catch((msg) => {
// this reject handler is not called because the promise chain is fulfilled now
console.log('reJECTed in catch()');
console.log(msg);
});
让我举一个例子来说明一个promise链如何切换状态:
Promise.reject("Hello").then(val => {
console.log("1: I am not called");
}).catch(err => {
console.log("2: I am rejected");
// rethrow to keep promise chain rejected
throw err;
}).catch(err => {
console.log("3: I am still rejected");
// return normal value, promise chain switches to fulfilled
return "GoodBye";
}).then(val => {
console.log("4: Now back to fulfilled state");
}).catch(err => {
console.log("5: Not called");
});
内容总结
以上是互联网集市为您收集整理的javascript – 将拒绝()跳过Promise中的所有关注then()全部内容,希望文章能够帮你解决javascript – 将拒绝()跳过Promise中的所有关注then()所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。