gpt4 book ai didi

node.js - 如何查找 Node.js UnhandledPromiseRejectionWarning 中哪些 Promise 未处理?

转载 作者:太空宇宙 更新时间:2023-11-04 02:02:19 25 4
gpt4 key购买 nike

版本 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 block 的情况下找到它?

最佳答案

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

关于node.js - 如何查找 Node.js UnhandledPromiseRejectionWarning 中哪些 Promise 未处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45540048/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com