当前位置:网站首页>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
边栏推荐
- [Energy Conservation Institute] Application of Intelligent Control Device in High Voltage Switchgear
- Questions I don't know in database kernel interview(1)
- C陷阱与缺陷 第7章 可移植性缺陷 7.11 可移植性问题的一个例子
- C陷阱与缺陷 附录B Koenig和Moo夫妇访谈
- 98.嵌入式控制器EC实战 EC开发板开发完成
- 附录A printf、varargs与stdarg A.3 stdarg.h ANSI版的varargs.h
- The Internet giant development process
- 30+的女性测试人面试经验分享
- 使用百度EasyDL实现厂区工人抽烟行为识别
- win10版本1803无法升级1903系统如何解决
猜你喜欢
扣减库存方案
JS提升:如何中断Promise的链式调用
Jmeter实战 | 同用户重复并发多次抢红包
[Energy Conservation Institute] Application of Intelligent Control Device in High Voltage Switchgear
StringTable Detailed String Pool Performance Tuning String Concatenation
写给刚进互联网圈子的人,不管你是开发,测试,产品,运维都适用
R语言 pca主成分分析的主要方法
有点奇怪!访问目的网址,主机能容器却不行
Nacos 配置中心
进行交互或动画时如何选择Visibility, Display, and Opacity
随机推荐
【Social Media Marketing】How to know if your WhatsApp is blocked?
基于FPGA的任意字节数(单字节、多字节)的串口(UART)发送(含源码工程)
tiup mirror clone
useful website
Go Atomic
虚拟机的IP地址自动变为127.0.0.1
线程池处理异常的方法
tiup mirror genkey
外骨骼机器人(七):标准步态数据库
Godaddy domain name resolution is slow and how to use DNSPod resolution to solve it
Excel advanced drawing techniques, 100 (22) - how to respectively the irregular data
【Kaggle】Classify Leaves
算法---解码方法(Kotlin)
latex paper artifact -- server deployment overleaf
Go Atomic
LeetCode每日一题(1807. Evaluate the Bracket Pairs of a String)
Digital twin Beijing the imperial palace, yuan universe is the process of tourism
The Internet giant development process
C专家编程 前言
扣减库存方案