本文介绍了陷阱when.js未经处理的拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想陷阱when.js未处理的拒绝,以便我可以记录它们。为了实现这一点,我已经覆盖了console.warn(),但是这可以记录除了我不感兴趣的when.js之外的东西。

I'd like to trap when.js unhandled rejections so that I can log them. To accomplish this I've overriden console.warn(), however that can log stuff other than when.js which I'm not interested in.

ref:

我正在使用prettymonitor with when.js

I am using prettymonitor with when.js https://github.com/AriaMinaei/pretty-monitor

推荐答案

如果你在在服务器端,您可以使用promise rejection hooks。这些将适用于服务器端的大多数承诺实现(io.js,bluebird,when等):

If you're on the server-side, you can use the promise rejection hooks. These will work on most promise implementations on the server-side (io.js, bluebird, when, etc):

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

如果您在浏览器环境中,事情就不那么标准化了。但是,当仍然提供类似的钩子时:

If you're in a browser environment, things are less standardised. However, When still provides similar hooks there:

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);

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

There are also local rejection hooks, these are good if you only want to handle rejections of a single instance of the promise library - this is typically useful for when building a library yourself:

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

这篇关于陷阱when.js未经处理的拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 16:16