当前位置:网站首页>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
边栏推荐
- pytest:开始使用
- latex paper artifact -- server deployment overleaf
- MySQL Syntax Basics
- 封装一个管理 url 状态的 hook
- Go 语言中常见的坑
- JS提升:手写发布订阅者模式(小白篇)
- Remove 360's detection and modification of the default browser
- 进行交互或动画时如何选择Visibility, Display, and Opacity
- 案例:MySQL主从复制与读写分离
- Which websites are commonly used for patent searches?
猜你喜欢
随机推荐
和我一起写一个音乐播放器,听一首最伟大的作品
外骨骼机器人(七):标准步态数据库
任务调度线程池-应用定时任务
写给刚进互联网圈子的人,不管你是开发,测试,产品,运维都适用
职场如象棋,测试/开发程序员如何突破成长瓶颈期?
【luogu P1912】诗人小G(二分栈)(决策单调性优化DP)
[Energy Conservation Institute] Application of Intelligent Control Device in High Voltage Switchgear
98. Embedded controller EC actual combat EC development board development completed
C专家编程 第1章 C:穿越时空的迷雾 1.4 K&R C
【Dart】dart构造函数学习记录(含dart单例模式写法)
tiup mirror merge
【Kaggle】Classify Leaves
有点奇怪!访问目的网址,主机能容器却不行
记录第一次给开源项目提 PR
4.1 配置Mysql与注册登录模块
Common pits in the Go language
LinkedList源码分享
tiup mirror init
MySQL 中出现的字符编码错误 Incorrect string value: ‘\x\x\x\x‘ for column ‘x‘
MySQL Syntax Basics