当前位置:网站首页>JS提升:如何中断Promise的链式调用
JS提升:如何中断Promise的链式调用
2022-08-01 21:00:00 【The..Fuir】
如何中断Promise的链式调用?
问题:当promise状态改变时,他的链式调用都会生效,那如果我们有这个一个实际需求:我们有5个then(),但其中有条件判断,如当我符合或者不符合第三个then条件时,要直接中断链式调用,不再走下面的then,该如何操作?
我们知道Promise有三种状态:pending(进行中)、fulfilled(已成功)和rejected(已失败),当状态从pending(进行中)变成fulfilled(已成功)或者rejected(已失败)的时候就会调用Promise的then方法, 如果一直在pending(进行中)状态的话,就不会执行到then方法了。
那我们就可以利用这一点去中断Promise的链式调用了,在回调函数中返回一个 pending(进行中) 状态的Promise 对象,这样后面的then方法就不会执行了。
这里的原理是利用了promise.then()返回的新 promise 的结果状态是由 then()指定的回调函数执行的结果决定。也就是说:
如果抛出异常, 新 promise 变为 rejected, reason 为抛出的异常
如果返回的是非 promise 的任意值, 新 promise 变为 resolved, value 为返回的值
如果返回的是另一个新 promise, 此 promise 的结果就会成为新 promise 的结果
所以我们在promise.then()返回了一个pending(进行中) 状态的Promise 对象,promise.then()返回的新 promise 的结果状态就一直是pending(进行中)的,后面的then方法也不会执行了。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);});
永不决议
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,'==='); })原文链接:https://blog.csdn.net/cckevincyh/article/details/124796139
边栏推荐
- 【微信小程序】【AR】threejs-miniprogram 安装(76/100)
- Pytorch框架学习记录13——利用GPU训练
- 线上问题排查常用命令,总结太全了,建议收藏!!
- 通俗解释:什么是临床预测模型
- Buttons with good user experience should not have hover state on mobile phones
- Interpretation of the meaning of each dimension of two-dimensional, three-dimensional, and four-dimensional matrices
- 响应式织梦模板美容整形类网站
- 扣减库存方案
- tiup mirror grant
- 模板特例化和常用用法
猜你喜欢

】 【 nn. The Parameter () to generate and why do you want to initialize

Excel advanced drawing techniques, 100 (22) - how to respectively the irregular data

Telnet弱口令渗透测试

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

KDD2022 | Self-Supervised Hypergraph Transformer Recommendation System

STAHL触摸屏维修一体机显示屏ET-316-TX-TFT常见故障

Postman 批量测试接口详细教程

Remove 360's detection and modification of the default browser

Zheng Xiangling, Chairman of Tide Pharmaceuticals, won the "2022 Outstanding Influential Entrepreneur Award" Tide Pharmaceuticals won the "Corporate Social Responsibility Model Award"

数据库单字段存储多个标签(位移操作)
随机推荐
虚拟机的IP地址自动变为127.0.0.1
Pytorch框架学习记录10——线性层
Pytorch框架学习记录12——完整的模型训练套路
Godaddy域名解析速度慢问题以及如何使用DNSPod解析解决
30+的女性测试人面试经验分享
自定义指令,获取焦点
[Multi-task model] Progressive Layered Extraction: A Novel Multi-Task Learning Model for Personalized (RecSys'20)
tiup mirror init
徒步,治好了我的精神内耗
tiup mirror grant
Protocol Buffer usage
LTE time domain and frequency domain resources
Little data on how to learn?Jida latest small learning data review, 26 PDF page covers the 269 - page document small data learning theory, method and application are expounded
wps excel 插入公式 整列
KDD2022 | Self-Supervised Hypergraph Transformer Recommendation System
如何用Chrome编辑以及调试代码
数据库单字段存储多个标签(位移操作)
tiup mirror genkey
职场如象棋,测试/开发程序员如何突破成长瓶颈期?
useful website
