当前位置:网站首页>JS提升:Promise中reject与then之间的关系
JS提升:Promise中reject与then之间的关系
2022-07-30 18:44:00 【The..Fuir】
直接看代码
let p2 = Promise.reject('err'); p2.then(() => { console.log('我永远不会被执行'); return 'abc' }).then((str) => { console.log('我也不会被执行!' + str); }, (err) => { console.log('err1'); }).catch((err) => { console.log('err2'); })
在我的记忆中。then可以定义两个参数,第一个为成功执行的函数,第二个为失败执行的函数。这样不就是 当我是reject的时候我还是可以执行then啊。只是执行的是第二个函数。
对失败的处理为止,或者找到catch()为止,期间的成功都不会执行。
如果then中只写成功的回调函数的话,就和老师说的一样了。resolve执行then回调,reject执行catch()回调。如果promise返回失败,then文件中又没有第二个参数,他就会继续向下找,直到找到then中const serialPromise=arr=>arr.reduce((pre,cur)=>pre.then(cur,reason=>{ console.log('PB的reject来到了这里'); return Promise.resolve(); }),Promise.resolve()) const pA=()=>new Promise((resolve,reject)=>{ console.log(1); resolve(); }) // 因为 pB 的时候 then 就丢掉了 const pB=()=>new Promise((resolve,reject)=>{ console.log(2); reject() }) const pC=()=>new Promise((resolve,reject)=>{ // console.log(3); resolve(); }) serialPromise([pA,pB,pC])
因为 pB 的时候 then 就丢掉了
边栏推荐
- 一文读懂“语言模型”
- Immersive experience iFLYTEK 2022 Consumer Expo "Official Designated Product"
- What kind of framework is friendly to developers?
- Network Basics (3) 01-Basic Concepts of Networks - Protocols, Host Addresses, Paths and Parameters of URL Addresses & 127.0.0.1 Local Loopback Address & View URL IP Address and Access Ping Space + URL
- 第十六期八股文巴拉巴拉说(MQ篇)
- 【Qt Designer工具的使用】
- The sixteenth issue of eight-part article Balabala said (MQ)
- LocalDate时间生成
- The use of terminal split screen tool Terminalx
- 《痞子衡嵌入式半月刊》 第 59 期
猜你喜欢
CCNA-NAT协议(理论与实验练习)
OneFlow源码解析:Op、Kernel与解释器
SwiftUI iOS Boutique Open Source Project Complete Baked Food Recipe App based on SQLite (tutorial including source code)
ESP8266-Arduino编程实例-HC-SR04超声波传感器驱动
(2022杭电多校四)1001-Link with Bracket Sequence II(区间动态规划)
载誉而归,重磅发布!润和软件亮相2022开放原子全球开源峰会
【Swords Offer】Swords Offer 17. Print n digits from 1 to the largest
Immersive experience iFLYTEK 2022 Consumer Expo "Official Designated Product"
Deepen school-enterprise cooperation and build an "overpass" for the growth of technical and skilled talents
NC | Tao Liang Group of West Lake University - TMPRSS2 "assists" virus infection and mediates the host invasion of Clostridium sothrix hemorrhagic toxin...
随机推荐
攻防世界web-Cat
Web结题报告
【剑指 Offe】剑指 Offer 18. 删除链表的节点
ROS 环境使用第三方动态链接库(.so)文件
CCNA-网络汇总 超网(CIDR) 路由最长掩码匹配
The large-scale application of artificial intelligence AI products in industrial-grade mature shipping ports of CIMC World Lianda will create a new generation of high-efficiency smart ports and innova
毕业1年从事软件测试拿下11.5k,没有给98后丢脸吧...
第14章 类型信息
NC | 西湖大学陶亮组-TMPRSS2“助攻”病毒感染并介导索氏梭菌出血毒素的宿主入侵...
网络基础(二)-Web服务器-简介——WampServer集成服务器软件之Apache+MySQL软件安装流程 & netstat -an之检测计算机的端口是否占用
微博广告分布式配置中心的构建与实践(有彩蛋)
CCNA-ACL(访问控制列表)标准ACL 扩展ACL 命名ACL
固定资产可视化智能管理系统
One year after graduation, I was engaged in software testing and won 11.5k. I didn't lose face to the post-98 generation...
CMake库搜索函数居然不搜索LD_LIBRARY_PATH
荐号 | 对你有恩的人,不要请吃饭来报答
WeChat Mini Program Cloud Development | Urban Information Management
MySQL data types
[Prometheus] An optimization record of the Prometheus federation [continued]
arcpy获取要素类(属性表)包含的数目