【问题标题】:How to find source of Possible Unhandled Promise Rejection [duplicate]如何找到可能的未处理承诺拒绝的来源[重复]
【发布时间】:2019-04-22 20:57:22
【问题描述】:

在处理大型继承代码库时,我偶然发现了这个不太有用的警告:

Possible Unhandled Promise Rejection (id: 0):
true

console.warn @ YellowBox.js:67
onUnhandled @ Promise.js:43
onUnhandled @ rejection-tracking.js:71
(anonymous) @ JSTimers.js:256
_callTimer @ JSTimers.js:152
callTimers @ JSTimers.js:405
__callFunction @ MessageQueue.js:349
(anonymous) @ MessageQueue.js:106
__guard @ MessageQueue.js:297
callFunctionReturnFlushedQueue @ MessageQueue.js:105
(anonymous) @ debuggerWorker.js:72

是否有可能获得导致此拒绝的承诺的文件和行号,而无需遍历整个代码并在任何地方添加 catch()?

【问题讨论】:

  • 这个生命周期方法应该可以帮到你componentDidCatch
  • 您是使用 fetch 还是 axis 来获取数据?使用 axios,您可以使用拦截器功能并在那里添加更多日志记录。我认为您也可以使用 fetch 来做到这一点。
  • @needsleep 应用程序使用 fetch,但问题适用于所有未处理的承诺

标签: react-native


【解决方案1】:

解决方案是使用第三方承诺库来保留额外的堆栈跟踪,如 Bluebird,如以下答案所述:https://stackoverflow.com/a/49129335/10236907

【讨论】:

    【解决方案2】:

    你可以在你的根组件中做这样的事情。

    componentDidCatch(error, info) {
        console.log(error)
        console.log(info)
    }
    

    不要记录您的错误,而是将其发送到您的数据库,以便您可以通过更多信息解决问题。

    Read more about componentDidCatch() life cycle method here

    【讨论】:

    • 不幸的是,这不会触发承诺拒绝
    猜你喜欢
    • 2016-11-24
    • 2018-03-04
    • 2020-07-21
    • 2016-12-15
    • 1970-01-01
    • 2023-01-04
    • 1970-01-01
    • 2020-04-04
    • 1970-01-01
    相关资源
    最近更新 更多