当前位置:网站首页>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 就丢掉了
边栏推荐
- core sound driver详解
- Recommendation | People who are kind to you, don't repay them by inviting them to eat
- What is the value of biomedical papers? How to translate the papers into Chinese and English?
- DTSE Tech Talk丨第2期:1小时深度解读SaaS应用系统设计
- After 23 years of operation, the former "China's largest e-commerce website" has turned yellow...
- (2022杭电多校四)1001-Link with Bracket Sequence II(区间动态规划)
- SwiftUI iOS Boutique Open Source Project Complete Baked Food Recipe App based on SQLite (tutorial including source code)
- What kind of framework is friendly to developers?
- MySql中@符号的使用
- Application of time series database in the field of ship risk management
猜你喜欢

博纳影通过IPO注册:阿里腾讯是股东 受疫情冲击明显

(2022杭电多校四)1001-Link with Bracket Sequence II(区间动态规划)

《痞子衡嵌入式半月刊》 第 59 期

延时队列优化 (2)

【总结】1396- 60+个 VSCode 插件,打造好用的编辑器

猎豹移动终于递交年报:年营收7.85亿 腾讯持股16.6%

Deepen school-enterprise cooperation and build an "overpass" for the growth of technical and skilled talents

scrapy基本使用

自然语言处理nltk

深化校企合作 搭建技术技能人才成长“立交桥”
随机推荐
DM8: Single database and single instance to build a local data guard service
ByteArrayInputStream 类源码分析
Recommendation | People who are kind to you, don't repay them by inviting them to eat
Critical Reviews | 南农邹建文组综述全球农田土壤抗生素与耐药基因分布
Multiple instances of mysql
JsonUtil基于字符串操作josn
Hello, my new name is "Bronze Lock/Tongsuo"
3D机器视觉厂商的场景争夺战役
Node encapsulates a console progress bar plugin
LeetCode Exercise - Two Questions About Finding Sum of Array Elements
CMake库搜索函数居然不搜索LD_LIBRARY_PATH
基于b/s架构搭建一个支持多路摄像头的实时处理系统 ---- 使用yolo v5 系列模型
MySQL data types
Deepen school-enterprise cooperation and build an "overpass" for the growth of technical and skilled talents
Quickly build an e-commerce platform based on Amazon cloud technology serverless service - performance
OMP: Error #15: Initializing libiomp5md.dll, but found libiomp5md.dll already initialized.解决方法
6块钱1斤,日本公司为何来中国收烟头?
软件测试13年从业经验的前辈,总结的5条测试就业建议....
Swiper轮播图片并播放背景音乐
OneFlow源码解析:Op、Kernel与解释器
