Finding source of unhandled promise rejection: TypeError: Chaining cycle detected for promise

Finding source of unhandled promise rejection: TypeError: Chaining cycle detected for promise

我正在尝试查找 Node.js

中 Promise 未处理拒绝的来源

我已经尝试升级到 Node 版本 12,使用 --async-stack-traces 选项,并使用以下方式监听它们:

process.on("unhandledRejection",( reason, promise ) => {
  console.log(reason);
  console.log(promise);
});

但我仍然没有看到任何有用的堆栈跟踪来帮助我找到罪魁祸首!

UnhandledPromiseRejectionWarning: TypeError: Chaining cycle detected for promise #<Promise>
    at process._tickCallback (internal/process/next_tick.js:68:7)
(node:89675) 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(). (rejection id: 11)

运行 节点 v10.10.0

为这段代码找到一些好的堆栈跟踪 const cyclic = Promise.resolve().then(() => cyclic); 我将此代码放入文件 prromise_cycle.js 和 运行 中,并使用检查器对其进行调试。

我在启用标志 Pause on caught exceptions 的 Chrome DevTools 上调试它,然后我可以看到整个堆栈跟踪以及我的文件

如果您错过了一个有用的堆栈跟踪,您可以通过在处理程序中重新抛出错误来让节点创建一个新堆栈跟踪,如下所示:

process.on('unhandledRejection', (reason, p) => { throw reason });

这样,你应该能找到罪魁祸首了。

感谢所有建议。我再次尝试升级到最新的节点 12.14.1 并最终能够显示堆栈跟踪:

我将 node --async-stack-traces myScript.js 与:

结合使用
process.on('unhandledRejection', (reason, p) => {
  console.log(reason);
});

并且它追踪到了错误。