【问题标题】:How do I find out where an unhandled promise rejection occured? [duplicate]如何找出未处理的 Promise 拒绝发生的位置? [复制]
【发布时间】:2017-11-03 20:16:06
【问题描述】:

快速前言:我了解 Promises,我了解解决和拒绝。这不是问题。

(node:14104) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: "callback" argument must be a function
(node:14104) [DEP0018] 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.\

我在我的代码中的某个地方理解了一个“回调”参数,它应该是一个函数,但不是。这也不是我的问题。

如何找到发生未处理拒绝的代码行?

【问题讨论】:

    标签: javascript node.js promise


    【解决方案1】:

    node 的'unhandledRejection' event 应该会解开你的谜团:

    process.on('unhandledRejection', (reason, promise) => {
        console.warn('Unhandled promise rejection:', promise, 'reason:', reason.stack || reason);
    });
    

    https://repl.it/I3JJ/2

    【讨论】:

    • 如果使用 reject() 函数拒绝了一个承诺,它将无法工作。
    • 工作就像一个魅力!谢谢。
    • @mikemaccana 欢迎你:)
    猜你喜欢
    • 2016-11-10
    • 1970-01-01
    • 2018-03-19
    • 1970-01-01
    • 2019-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-28
    相关资源
    最近更新 更多