我现在可以得到未来未处理的承诺拒绝行为吗?

我现在可以得到未来未处理的承诺拒绝行为吗?

问题描述:

将来,未处理的 promise 拒绝将以非零退出代码终止 Node.js 进程.

In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

我的管道在它应该失败的时候通过了,并部署了一个在启动时崩溃的版本.如果 Node.js 会以非零退出代码退出,则管道将失败并且不会部署错误版本.

My pipeline passed when it should have failed and deployed a version that is crashing on launch. If Node.js would have exited with a non-zero exit code, the pipeline would have failed and the bad version wouldn't have been deployed.

有没有办法让 Node.js 在遇到未处理的承诺拒绝时以非零退出代码退出,而不需要我等待未来?

Is there a way to make Node.js exit with a non-zero exit code when it encounters an unhandled promise rejection, that doesn't require me to wait for the future?

是的,你可以,使用 process 对象上的 unhandledRejection 事件:

Yes, you can, using the unhandledRejection event on the process object:

process.on('unhandledRejection', (reason, p) => {
  console.error('Unhandled Rejection at:', p, 'reason:', reason)
  process.exit(1)
});