本文介绍了如何找到 Node.js UnhandledPromiseRejectionWarning 中未处理的承诺?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Node.js 从版本 7 开始使用 async/await 语法糖来处理 promise,现在在我的代码中经常出现以下警告:

Node.js from version 7 has async/await syntactic sugar for handling promises and now in my code the following warning comes up quite often:

(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.

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

Unfortunately there's no reference to the line where the catch is missing.Is there any way to find it without checking every try/catch block?

推荐答案

listen unhandledRejection 进程事件.

listen unhandledRejection event of process.

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

这篇关于如何找到 Node.js UnhandledPromiseRejectionWarning 中未处理的承诺?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 20:59