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");})
相关推荐
冷雨夜中漫步6 小时前
Claude Code源码分析——Claude Code Agent Loop 详细设计文档
java·开发语言·人工智能·ai
超龄编码人6 小时前
Qt Widgets Designer QTabWidget无法添加布局
开发语言·qt
直奔標竿6 小时前
Java开发者AI转型第二十六课!Spring AI 个人知识库实战(五)——联网搜索增强实战
java·开发语言·人工智能·spring boot·后端·spring
Python大数据分析@6 小时前
CLI一键采集,使用Python搭建TikTok电商爬虫Agent
开发语言·爬虫·python
@小码农7 小时前
2026年3月Scratch图形化编程等级考试一级真题试卷
开发语言·数据结构·c++·算法
这儿有一堆花7 小时前
住宅代理(Residential Proxy)技术指南
开发语言·数据库·php
一只大袋鼠7 小时前
Java进阶:CGLIB动态代理解析
java·开发语言
秦ぅ时7 小时前
保姆级教程|OpenAI tts-1-hd模型调用全流程(Python+curl+懒人用法)
开发语言·python
Eiceblue7 小时前
使用 C# 将 Excel 转换为 Markdown 表格(含批量转换示例)
开发语言·c#·excel
爱滑雪的码农7 小时前
Java基础十三:Java中的继承、重写(Override)与重载(Overload)详解
java·开发语言