node.js 中怎样解决 UnhandledPromiseRejectionWarning 错误?

以你之姓@ 2022-10-25 12:47 1353阅读 0赞

在 node.js 运行中,当遇到 UnhandledPromiseRejectionWarning 错误或警告时,该如何解决呢?错误提示有时候是这样的:

(node:6603) UnhandledPromiseRejectionWarning: Error: Protocol error (Runtime.callFunctionOn): Object reference chain is too long

有时候是这样的:

(node:6603) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch().

问题在于未捕捉的异常,解决办法就是加一个处理句柄,专门处理这个抛出错误(promise rejection):

  1. process.on('unhandledRejection', (reason, p) => {
  2. console.log('Promise: ', p, 'Reason: ', reason)
  3. // do something
  4. })

发表评论

表情:
评论列表 (有 0 条评论,1353人围观)

还没有评论,来说两句吧...

相关阅读