如何在 Node.js UnhandledPromiseRejectionWarning 中找到未处理的承诺?

新手上路,请多包涵

版本 7 的 Node.js 具有用于处理 Promise 的 async/await 语法糖,现在在我的代码中经常出现以下警告:

 (node:11057) UnhandledPromiseRejectionWarning: Unhandled promise
rejection (rejection id: 1): ReferenceError: Error: Can't set headers
after they are sent.
(node:11057) 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.

不幸的是,没有提到 catch 丢失的行。有没有办法在不检查每个 try/catch 块的情况下找到它?

原文由 user1658162 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 745
1 个回答

unhandledRejection 进程事件。

 process.on('unhandledRejection', (reason, p) => {
  console.log('Unhandled Rejection at: Promise', p, 'reason:', reason);
  // application specific logging, throwing an error, or other logic here
});

原文由 cuixiping 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题