我现在可以得到未来未处理的承诺拒绝行为吗? [英] Can I get the future unhandled promise rejection behaviour now?

查看:43
本文介绍了我现在可以得到未来未处理的承诺拒绝行为吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将来,未处理的 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)
});

这篇关于我现在可以得到未来未处理的承诺拒绝行为吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆