在诺言创建后添加finally处理程序会导致未捕获的诺言拒绝?

问题描述

如果我有这样的脚本

function timeout(ms) {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve();
    },ms);
  });
}

(async () => {
  try {
    async function throwAfterOneSecond() {
      await timeout(1000);
      throw new Error("wow");
    }

    const p1 = throwAfterOneSecond()
    p1.finally(() => {
      console.log("finally handler");
    });
    await p1;
  } catch (e) {
    console.error(e);
  }
})();

然后运行它,这将导致未处理的promise异常


$ node test.js
finally handler
Error: wow
    at throwAfterOneSecond (/home/cdiesh/test.js:13:13)
(node:3657795) UnhandledPromiseRejectionWarning: Error: wow
    at throwAfterOneSecond (/home/cdiesh/test.js:13:13)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:3657795) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block,or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection,use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:3657795) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future,promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

但是如果我使用

    const p1 = throwAfterOneSecond().finally(() => {
      console.log("finally handler");
    });

没有未处理的promise例外,一切都很好

此行为有名字吗?运行节点v14.7.0

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...