promise的catch放在then前面的场景

我们知道Promise 的 .then() 和 .catch() 方法的链式调用顺序通常是先 .then() 后 .catch()。这是因为 .then() 方法用于处理成功的情况,而 .catch() 方法用于处理失败的情况。.catch() 方法会捕获前面所有 .then() 链中抛出的异常,以及在 Promise 执行过程中发生的任何错误。

在一个失败重试场景中我把catch放在then前面,代码逻辑会很简单:

javascript 复制代码
...
const pause = (duration) => new Promise((reslove) => setTimeout(reslove, duration));
...
dosomething()
	.catch(e=>{
		console.log("frist fail");
		pause(delay).then(()=>{
			console.log("second try"); 
			dosomething()
				.then(()=>{console.log("retry succ");});
			})
	.then(()=>{dootherthing();})
	.catch(e=>{console.log("fail");})
相关推荐
等我调个Bug14 小时前
C++智能指针通俗讲解
开发语言·c++
黑化旺仔14 小时前
【OC】Runtime:类元数据、加载初始化与消息派发
开发语言·macos·ios·objective-c
Felven14 小时前
C. Removing Smallest Multiples
c语言·开发语言
云和数据.ChenGuang14 小时前
光哥说Python 循环导入(Circular Import)深度解析:成因、排查与解决方案
开发语言·前端·人工智能·python·langchain
星夜离尘5614 小时前
MATLAB 工程实验:滞回加持续时间报警,把 UNKNOWN 留在状态机里
开发语言·matlab
fengci.15 小时前
BUUCTF(困难部分)
android·开发语言·前端·学习·php
Highcharts.js15 小时前
BI可视化图表“维护难”问题的解决方案
开发语言·前端·javascript·编辑器·ecmascript·bi·highcharts
倚窗听花_15 小时前
IIS部署前端Vue项目
前端·javascript·vue.js
yixun_gdas15 小时前
政企网站错敏巡查:错敏都藏在哪些位置?
开发语言·前端·javascript·内容运营
小羊没烦恼!15 小时前
.NET 11 Runtime Async 详解
java·运维·开发语言·javascript·python