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");})
相关推荐
by__csdn几秒前
Ajax与Axios终极对比指南全方位对比解析
前端·javascript·ajax·okhttp·typescript·vue·restful
开发者小天1 分钟前
React中的 css in js的使用示例
javascript·css·react.js
khatung2 分钟前
借助Electron打通平台与用户通知(macOS系统)
前端·javascript·vscode·react.js·macos·electron·前端框架
洲星河ZXH2 分钟前
Java,String类
java·开发语言
xcLeigh4 分钟前
【新】Rust入门:基础语法应用
开发语言·算法·rust
冬夜戏雪4 分钟前
【Java学习日记】【2025.12.2】【2/60】
java·开发语言·学习
小年糕是糕手7 分钟前
【C++同步练习】类和对象(一)
java·开发语言·javascript·数据结构·c++·算法·排序算法
txxzjmzlh7 分钟前
类和对象(下)
开发语言·c++
运维小文7 分钟前
Centos7部署.net8和升级libstdc++
开发语言·c++·.net
by__csdn8 分钟前
Vue3+Axios终极封装指南
前端·javascript·vue.js·http·ajax·typescript·vue