从版本 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 块的情况下找到它?
监听unhandledRejection进程事件。
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 });