我不能覆盖Promise的then()函数吗?

时间:2019-03-29 08:57:39

标签: javascript es6-promise

我正在尝试收集ES6 Promise中引发的错误。

我想到了一种方法来覆盖Promise.prototype.then函数。这是我的代码:

let originalThen = Promise.prototype.then;
Promise.prototype.then = function(onResolved, onRejected) {
    return originalThen.call(this, onResolved, onRejected).catch(error => {
        console.log(`upload the error to server: ${ error }`);
        throw error;
    })
}
new Promise((resolve, reject) => {
    resolve('something');
})
.then(value => {
    // I think this error should be caught and upload to server.
    throw new Error('test error')
}).catch(error => {
    //the previous error should also be caught here.
    console.log('caught error in catch()')
})

当我在Firefox中运行此代码时,出现了"too much recursion"错误。日志语句"upload the error to server"一遍又一遍!但是我没有对代码进行任何递归。发生什么事了?

1 个答案:

答案 0 :(得分:0)

问题在于.catch(onReject)只是.then(undefined, onReject)的语法糖,catch方法只是调用then的包装器。从覆盖的then中调用它确实会导致(无限次)递归。

相反,您需要使用originalThen来捕获拒绝:

let originalThen = Promise.prototype.then;
Promise.prototype.then = function(onResolved, onRejected) {
    return originalThen.call(originalThen.call(this, onResolved, onRejected), undefined, error => {
        console.log(`upload the error to server: ${ error }`);
        throw error;
    });
}

也就是说,我认为覆盖then是一个坏主意。

  

我正在尝试收集ES6 Promise中引发的错误。

为此,最好通过unhandledrejection events收集错误。您的许多诺言链都会故意使用错误并稍后在本地进行处理-您只想将意外错误记录到服务器中。