一尘不染

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

node.js

版本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.

不幸的是,没有提到丢失渔获物的那一行。有没有找到所有方法而不检查每个try / catch块的方法?


阅读 741

收藏
2020-07-07

共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
});
2020-07-07