gpt4 book ai didi

javascript - 陷阱 when.js 未处理的拒绝

转载 作者:数据小太阳 更新时间:2023-10-29 05:26:01 25 4
gpt4 key购买 nike

我想捕获 when.js 未处理的拒绝,以便我可以记录它们。为此,我覆盖了 console.warn(),但是它可以记录我不感兴趣的 when.js 以外的内容。

引用:https://github.com/cujojs/when/blob/master/docs/api.md#debugging-promises

我在 when.js 中使用 prettymonitor https://github.com/AriaMinaei/pretty-monitor

最佳答案

如果您在服务器端,您可以使用 promise 拒绝钩子(Hook)。这些将适用于服务器端的大多数 promise 实现(io.js、bluebird、when 等):

 process.on("unhandledRejection", function(promise, reason){
// deal with the rejection here.
});

如果您处于浏览器环境中,那么事情就不那么标准化了。但是,When 仍然在那里提供类似的钩子(Hook):

window.addEventListener('unhandledRejection', function(event) {
event.preventDefault(); // This stops the initial log.
// handle event
event.detail.reason; // rejection reason
event.detail.key; // rejection promise key
}, false);

还有本地拒绝 Hook ,如果您只想处理对 promise 库的单个实例的拒绝,这些 Hook 非常有用 - 这通常在您自己构建库时很有用:

var Promise = require('when').Promise;
Promise.onPotentiallyUnhandledRejection = function(rejection) {
// handle single instance error here
};

关于javascript - 陷阱 when.js 未处理的拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29689143/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com