【发布时间】:2018-01-23 20:11:03
【问题描述】:
我试图拦截所有 Promises then 方法的响应。但我无法在原型 then 方法中获取响应数据。请找到下面的代码。
(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)
在上面的代码中,我可以看到控制台打印在所有 Promise 调用中。但是我无法在 then 中获取响应对象。
控制台中打印的“this”对象值:
“then”原型方法中的打印参数:
请建议我在 then 方法中为所有 promises 方法获取响应对象。
我尝试使用“arguments[0].arguments”(然后回调中的响应对象)获取值。但是它抛出了以下错误
Uncaught TypeError: 'caller' 和 'arguments' 是受限函数 属性,并且无法在此上下文中访问。
请建议我拦截响应对象的解决方案。
提前致谢。
【问题讨论】:
-
您愿意详细说明原因吗?
标签: javascript reactjs ecmascript-6 promise es6-promise