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");})
相关推荐
励志不掉头发的内向程序员19 分钟前
【从零写一个CAD 03】三个 double 值得单独一个类吗:把视图变换抽成 View
开发语言·c++·qt·学习·系统架构
PHP实战开发录32 分钟前
PHP接口大整数为什么变了
开发语言·php
临床数据科学和人工智能兴趣组4 小时前
在R语言中, 使用 as.factor() 函数转换数值型变量
开发语言·r语言
andyweike7 小时前
php笔记
开发语言·笔记·php
Circ.8 小时前
Python 调试 Litellm 本地大模型接口:模型列表与对话接口实操踩坑记录
开发语言·python
现任明教教主~10 小时前
Thinkphp站群蜘蛛池SaaS系统YanyvSEO含多用户/积分/六大引擎计费/易支付对接
java·开发语言·spring
Zhou14113610 小时前
SpringSecurity_02_授权与高级功能
开发语言·python
码匠许师傅10 小时前
【C++三方组件】cpp-httplib:一个头文件起 HTTP 服务
开发语言·c++·http