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");})
相关推荐
漂流瓶jz6 小时前
Webpack如何实现万物皆可import?loader的使用/配置/手写实践
前端·javascript·webpack
ZC跨境爬虫6 小时前
跟着 MDN 学CSS day_41:显式轨道、隐式网格与区域命名放置
前端·javascript·css·ui·交互
石山代码6 小时前
ArrayList / HashMap / ConcurrentHashMap
java·开发语言
程序大视界7 小时前
【Python系列课程】Python正则表达式(下):环视、命名分组与日志实战
开发语言·python·正则表达式
枫叶v.7 小时前
Agent 分层存储架构设计:从记忆方法到中间件选型
开发语言·python
Moment8 小时前
长上下文会最终杀死 Rag 吗?
前端·javascript·后端
sleven fung9 小时前
MinerU与BabelDOC与KTransformers与OpenAI API库
开发语言·python·ai·langchain
萤萤七悬9 小时前
【Python笔记】AI帮实现CLI工具-使用argparse.ArgumentParser接收命令参数
开发语言·笔记·python
iCxhust9 小时前
C# 命令行指令 查看二进制文件
开发语言·单片机·嵌入式硬件·c#·proteus·微机原理·8088单板机
csdn_aspnet9 小时前
Java 霍尔分区算法(Hoare‘s Partition Algorithm)
java·开发语言·算法