Skip to content

promise 关于 then 和 catch 的链接问题

下面输出:1 3

js
Promise.resolve()
  .then(() => {
    console.log(1); // 1 resolved 触发 then
  })
  .catch(() => {
    console.log(2); // 不触发
  })
  .then(() => {
    console.log(3); // 3
  }); // resolved

下面输出:1 2 3

js
Promise.resolve()
  .then(() => {
    console.log(1); // 1
    throw new Error('error1'); // rejected 触发 catch
    console.log(4); // 不执行
  })
  .catch(() => {
    console.log(2); // 2 resolved 触发 then
  })
  .then(() => {
    console.log(3); // 3
  }); // resolved

下面输出:1 2

js
Promise.resolve()
  .then(() => {
    console.log(1); // 1
    throw new Error('error1'); // rejected 触发 catch
  })
  .catch(() => {
    console.log(2); // 2 resolved 触发 then
  })
  .catch(() => {
    // 注意这里是 catch
    console.log(3); // 不触发
  }); // resolved

基于 MIT 许可发布