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

延时队列优化 (2)

ESP8266-Arduino编程实例-BMP180气压温度传感器驱动

Scrapy framework is introduced

432.4 FPS 快STDC 2.84倍 | LPS-Net 结合内存、FLOPs、CUDA实现超快语义分割模型

Basic use of scrapy

Quickly build an e-commerce platform based on Amazon cloud technology serverless service - performance

Critical Reviews | 南农邹建文组综述全球农田土壤抗生素与耐药基因分布

Mysql执行原理剖析

开心的聚餐

cocos creater 热更重启导致崩溃
随机推荐
高精度加法
node封装一个控制台进度条插件
毕业1年从事软件测试拿下11.5k,没有给98后丢脸吧...
The use of terminal split screen tool Terminalx
一文读懂“语言模型”
ESP8266-Arduino编程实例-DS18B20温度传感器驱动
Does the satellite phone communicate directly with the satellite or through a ground station?
自己需要努力
智慧中控屏
nlohmann json 使用指南【visual studio 2022】
软件测试13年从业经验的前辈,总结的5条测试就业建议....
[TypeScript]编译配置
Immersive experience iFLYTEK 2022 Consumer Expo "Official Designated Product"
NC | 西湖大学陶亮组-TMPRSS2“助攻”病毒感染并介导索氏梭菌出血毒素的宿主入侵...
Swiper轮播图片并播放背景音乐
ByteArrayInputStream class source code analysis
WeChat Mini Program Cloud Development | Urban Information Management
After 23 years of operation, the former "China's largest e-commerce website" has turned yellow...
微博广告分布式配置中心的构建与实践(有彩蛋)
中集世联达工业级成熟航运港口人工智能AI产品规模化应用,打造新一代高效能智慧港口和创新数字港口,全球港航人工智能能领军者中集飞瞳
