我想捕获when.js 未处理的拒绝,以便我可以记录它们。为了实现这一点,我重写了 console.warn(),但是它可以记录除 when.js 以外的我不感兴趣的内容。
ref: https://github.com/cujojs/when/blob/master/docs/api.md#debugging-promises https://github.com/cujojs/when/blob/master/docs/api.md#debugging-promises
我将 PrettyMonitor 与when.js 一起使用https://github.com/AriaMinaei/pretty-monitor https://github.com/AriaMinaei/pretty-monitor
如果您在服务器端,则可以使用承诺拒绝挂钩。这些将适用于服务器端的大多数 Promise 实现(io.js、bluebird、when 等):
process.on("unhandledRejection", function(promise, reason){
// deal with the rejection here.
});
如果您处于浏览器环境中,则标准化程度较低。然而,When 仍然提供类似的钩子:
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);
还有local拒绝挂钩,如果您只想处理承诺库的单个实例的拒绝,那么这些是很好的 - 这通常在您自己构建库时很有用:
var Promise = require('when').Promise;
Promise.onPotentiallyUnhandledRejection = function(rejection) {
// handle single instance error here
};
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)