You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何Promise中.catch后的.then会执行?代码返回reject仍触发后续then

问题:Promise.reject()后为何后续两个.then仍会执行?

即使返回Promise.reject(),最后两个.then仍会被调用。第一个.then()输出undefined,第二个.then()也输出结果。能否有人解释其中原因?

代码示例:

let p4 = new Promise((resolve,reject)=>{
    resolve("p4 Resolved");
});

p4.then((val)=>{
    console.log(val);
    return Promise.reject("20 Rejected")
}).catch((error)=>{console.log(error)})
  .then((val)=>{console.log(val);return 290})
  .then((val)=>{console.log("Last then : "+val);});
原因解析

这事儿的核心是Promise链里的.catch()执行完后,默认会返回一个状态为resolved的新Promise,除非你在.catch()里主动抛错或者返回rejected状态的Promise。

咱们一步步捋执行流程:

  • 一开始p4被resolve,第一个.then里打印p4 Resolved,然后返回一个rejected的Promise,值是"20 Rejected"。
  • 紧接着的.catch捕获到这个错误,打印20 Rejected。重点来了:这个.catch函数没写return语句,默认返回undefined,所以它会生成一个resolved状态的Promise,值就是undefined。
  • 下一个.then接收到这个resolved的Promise,参数val就是undefined,所以打印undefined,然后返回290,这又生成一个resolved的Promise,值是290。
  • 最后一个.then拿到290,打印Last then : 290。

说白了,.catch处理完错误后,整个链就回到正常的resolved状态了,后面的.then当然会继续执行。要是想让.catch之后的链停掉,得在.catch里主动throw错误或者返回Promise.reject()才行。

内容的提问来源于stack exchange,提问作者Ankur Dahiya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 18:45:10