当前位置:网站首页>JS hoisting: how to break the chain of Promise calls
JS hoisting: how to break the chain of Promise calls
2022-08-01 21:03:00 【The..Fuir】
How to break the chain of Promise calls?
Problem: When the state of promise changes, his chained calls will take effect, then if we have this actual requirement: we have 5 then(), but there are conditional judgments, such as when I meet or not meet the firstWhen there are three then conditions, how to directly interrupt the chain call and no longer go to the following then?
We know that Promise has three states: pending (in progress), fulfilled (successful) and rejected (failed), when the state changes from pending (in progress)) becomes fulfilled (successful) or rejected (failed), the then method of Promise will be called. If it has been in the pending (in progress) state, the then method will not be executed.
Then we can use this to interrupt the chained call of Promise, and return a Promise object in the pending (in progress) state in the callback function, so that the then method will notimplemented.
The principle here is that the result state of the new promise returned by promise.then() is determined by the result of the callback function specified by then().That is:
If an exception is thrown, the new promise becomes rejected, and the reason is the thrown exception
If any value other than a promise is returned, the new promise becomes resolved, and value is the returned value
IfWhat is returned is another new promise, and the result of this promise will become the result of the new promise
So we return a promise object in the pending (in progress) state in promise.then(), The result status of the new promise returned by promise.then() is always pending (in progress), and the then method will not be executed.let p = new Promise((resolve, reject) => {setTimeout(() => { resolve('OK');}, 1000);});p.then(value => {return new Promise(() => {});}).then(value => { console.log(222);}).then(value => { console.log(333);}).catch(reason => {console.warn(reason);});
Never decide
new Promise(resolve=>{resolve(1);}).then(value=>{console.log(value)return 2}).then(value=>{console.log(2)return new Promise(()=>{})}).then(value=>{console.log(value);return 4;}).then(value=>{console.log(value);return 5}).catch(error=>{console.log(error,'===');})Original link: https://blog.csdn.net/cckevincyh/article/details/124796139
边栏推荐
猜你喜欢
随机推荐
C专家编程 第1章 C:穿越时空的迷雾 1.1 C语言的史前阶段
[译] 容器和 Kubernetes 中的退出码完整指南
Hangao data import
Goroutine Leaks - The Forgotten Sender
98.嵌入式控制器EC实战 EC开发板开发完成
Interview Blitz 70: What are sticky packs and half packs?How to deal with it?
Godaddy域名解析速度慢问题以及如何使用DNSPod解析解决
技能大赛训练:A部分加固题目
R语言 数据的关系探索
网络安全与基础设施安全局(CISA):两国将在网络安全方面扩大合作
C陷阱与缺陷 附录B Koenig和Moo夫妇访谈
98. Embedded controller EC actual combat EC development board development completed
What is the difference between a utility model patent and an invention patent?Understand in seconds!
JS提升:手写发布订阅者模式(小白篇)
Go 语言中常见的坑
如何让定时器在页面最小化的时候不执行?
通俗解释:什么是临床预测模型
R语言进行相关的操作
外骨骼机器人(七):标准步态数据库
Postman 批量测试接口详细教程










