chrome 53没有抓住未处理的被拒绝的承诺

时间:2016-10-21 09:31:34

标签: javascript google-chrome promise bluebird superagent

我在开发时使用chrome 53,某个API调用会返回拒绝的bluebird承诺。虽然我的代码中有错误处理程序,但是被拒绝的承诺没有被捕获,只是安慰。

API调用:

function call() {
    return new Promise((resolve, reject) => {
        superagent
            .get('some url')
            .end((err, res) => {
                if(err) {
                    return reject(err);
                }
                resolve(res);
            });
    });
}

和错误处理程序:

window.onerror = ((message, filename, lineno, colno, error) => {
    const {unhandledErrors} = this.state;

    if(!error) {
        return;
    }

    error.timestamp = moment().format('YYYY-MM-DD HH:mm:ss');

    //handling error in React
    this.setState({
        unhandledErrors: updateUnhandledErrors(unhandledErrors, error)
    });

    console.error(error);
});

window.onunhandledrejection = (({reason}) => {
    const {unhandledErrors} = this.state;
    const error = reason;
    error.timestamp = moment().format('YYYY-MM-DD HH:mm:ss');

    //handling error in React
    this.setState({
        unhandledErrors: updateUnhandledErrors(unhandledErrors, error)
    });

    console.error(error);
});

// bluebird handler
window.addEventListener('unhandledrejection', e => {
    e.preventDefault();
    const {unhandledErrors} = this.state;

    const error = e.detail.reason;
    error.timestamp = moment().format('YYYY-MM-DD HH:mm:ss');

    //handling error in React
    this.setState({
        unhandledErrors: updateUnhandledErrors(unhandledErrors, error)
    });

    console.error(error);
});

适用于Firefox 31和Chromium 35

1 个答案:

答案 0 :(得分:0)

感谢@JaromandaX的评论,我认为我忘了用bluebird覆盖本机承诺。

相关问题