我试图拦截所有的承诺,然后方法的反应。但是,我无法在原型中得到响应数据,然后才是方法。请找到下面的代码。
(function(Promise) {
var originalThen = Promise.prototype.then;
var originalCatch = Promise.prototype.catch;
Promise.prototype.then = function() {
console.log(this, arguments);
return originalThen.apply(this, arguments);
};
Promise.prototype.catch = function() {
return originalCatch.apply(this, arguments);
};
})(this.Promise)在上面的代码中,我可以看到在所有承诺调用中打印的控制台。但是,我不能得到响应对象在当时。
控制台中打印的“this”对象值:

‘the’prototype方法中打印的参数:

请建议我在所有承诺方法的方法中得到响应对象。
我尝试使用"arguments.arguments“(然后回调中的响应对象).But来获取值,它引发了下面的错误
Uncaught:“调用者”和“参数”是受限制的函数属性,不能在此上下文中访问。
请建议我拦截响应对象的解决方案。
提前谢谢。
发布于 2017-08-15 20:34:32
then是一种注册成功和失败回调的同步方法。它马上就会回来。
若要拦截未来值,请插入自己以代替成功回调:
(function(Promise) {
var originalThen = Promise.prototype.then;
Promise.prototype.then = function(onFulfilled, onFailure) {
return originalThen.call(this, function(value) {
console.log(value);
return onFulfilled(value);
}, onFailure);
};
})(this.Promise);
Promise.resolve(3).then(() => console.log("Done"));
https://stackoverflow.com/questions/45700756
复制相似问题