Can I get the future unhandled promise rejection behaviour now?

只愿长相守 提交于 2020-05-29 22:53:08

问题


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

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.

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?


回答1:


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)
});



回答2:


For node 12 and later:

node --unhandled-rejections=strict


来源:https://stackoverflow.com/questions/51632965/can-i-get-the-future-unhandled-promise-rejection-behaviour-now

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!