当前位置:网站首页>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
边栏推荐
猜你喜欢

Based on FPGA in any number of bytes (single-byte or multibyte) serial port (UART) to send (including source engineering)
Godaddy domain name resolution is slow and how to use DNSPod resolution to solve it

Pytorch学习记录(八):生成对抗网络GAN

PyTorch笔记 - Attention Is All You Need (2)

R语言 数据的关系探索

Pytorch框架学习记录8——最大池化的使用

【Kaggle】Classify Leaves

Jmeter实战 | 同用户重复并发多次抢红包

R语言 线性回归的有关方法

Imitation cattle forum project
随机推荐
人工智能可信安全与评测
Buttons with good user experience should not have hover state on mobile phones
【luogu P1912】诗人小G(二分栈)(决策单调性优化DP)
如何让定时器在页面最小化的时候不执行?
用户身份标识与账号体系实践
Telnet弱口令渗透测试
[译] 容器和 Kubernetes 中的退出码完整指南
【Dart】dart之mixin探究
C语言之字符串函数二
面试官:大量请求 Redis 不存在的数据,从而打倒数据库,有什么方案?
tiup mirror merge
98. Embedded controller EC actual combat EC development board development completed
技术栈概览
JS提升:如何中断Promise的链式调用
C陷阱与缺陷 第7章 可移植性缺陷 7.7 除法运算时发生的截断
STAHL触摸屏维修一体机显示屏ET-316-TX-TFT常见故障
tiup mirror grant
封装一个管理 url 状态的 hook
Remove 360's detection and modification of the default browser
【Kaggle】Classify Leaves
