我可以让nodejs终止未处理的拒绝承诺吗?

And*_*rew 4 node.js

我的代码中有一个错误,它触发了警告:

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.

当这些发生时,有没有办法让Node终止?

Ry-*_*Ry- 7

process将为这些发出一个unhandledRejection事件,所以:

process.on('unhandledRejection', error => {
    throw error;
});
Run Code Online (Sandbox Code Playgroud)

  • 而不是“抛出错误;”也许“process.exit(1)”会更好。只是为了跟踪将来会发生什么,正如消息所说:“Node.js 进程具有非零退出代码。” (3认同)
  • @DiegoZoracKy:引发错误已经导致退出代码为非零。它还显示有用的信息。 (2认同)