如何找到未在Node

如何找到未在Node

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

问题描述

限时删除!!

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

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?

推荐答案

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中处理的承诺?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 16:16