当前位置:网站首页>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
边栏推荐
- R语言进行相关的操作
- latex paper artifact -- server deployment overleaf
- MySQL Syntax Basics
- STAHL touch screen repair all-in-one display screen ET-316-TX-TFT common faults
- Godaddy domain name resolution is slow and how to use DNSPod resolution to solve it
- tiup mirror
- 4.1 配置Mysql与注册登录模块
- C专家编程 第1章 C:穿越时空的迷雾 1.5 今日之ANSI C
- C专家编程 第1章 C:穿越时空的迷雾 1.4 K&R C
- 仿牛客论坛项目
猜你喜欢
随机推荐
The Internet giant development process
pytest:开始使用
C陷阱与缺陷 附录B Koenig和Moo夫妇访谈
LinkedList源码分享
如何封装 cookie/localStorage/sessionStorage hook?
JS提升:如何中断Promise的链式调用
C语言之字符串函数二
C陷阱与缺陷 第7章 可移植性缺陷 7.7 除法运算时发生的截断
Pytorch框架学习记录12——完整的模型训练套路
C专家编程 第1章 C:穿越时空的迷雾 1.4 K&R C
算法---解码方法(Kotlin)
移植MQTT源码到STM32F407开发板上
98. Embedded controller EC actual combat EC development board development completed
这些 hook 更优雅的管理你的状态
宝塔搭建PESCMS-Ticket开源客服工单系统源码实测
系统收集集
The configuration manual for the secondary development of the XE training system of the missing moment document system
织梦模板加入php代码
Common pits in the Go language
string










