为什么我不能一个Promise.catch处理器中扔? [英] Why can I not throw inside a Promise.catch handler?

查看:668
本文介绍了为什么我不能一个Promise.catch处理器中扔?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我不能只是抛出一个错误渔获回调里面,让工艺处理错误,如果它是在任何其他范围?

Why can't I just throw an Error inside the catch callback and let the process handle the error as if it were in any other scope?

如果我不这样做的console.log(ERR)没有东西打印出来,我什么都不知道发生了什么事。这个过程只是结束...

If I don't do console.log(err) nothing gets printed out and I know nothing about what happened. The process just ends...

例如:

function do1() {
    return new Promise(function(resolve, reject) {
        throw new Error('do1');
        setTimeout(resolve, 1000)
    });
}

function do2() {
    return new Promise(function(resolve, reject) {
        setTimeout(function() {
            reject(new Error('do2'));
        }, 1000)
    });
}

do1().then(do2).catch(function(err) {
    //console.log(err.stack); // This is the only way to see the stack
    throw err; // This does nothing
});

如果回调获得在主线程中执行,为什么错误得到由一个黑洞吞噬?

If callbacks get executed in the main thread, why the Error gets swallowed by a black hole?

推荐答案

正如其他人所解释的那样,黑洞,是因为里面扔了 .catch 继续链用拒绝承诺,你没有更多的渔获物,导致无端接链,这燕子错误(坏了!)

As others have explained, the "black hole" is because throwing inside a .catch continues the chain with a rejected promise, and you have no more catches, leading to an unterminated chain, which swallows errors (bad!)

再添加一个捕获,看看发生了什么:

Add one more catch to see what's happening:

do1().then(do2).catch(function(err) {
    //console.log(err.stack); // This is the only way to see the stack
    throw err; // Where does this go?
}).catch(function(err) {
    console.log(err.stack); // It goes here!
});

当你想链条,尽管失败的步骤进行了链的中间的catch是有用的,但再次引发非常有用的继续失败的做这样的事情的记录后,信息或清理步骤,甚至改变它引发错误。

A catch in the middle of a chain is useful when you want the chain to proceed in spite of a failed step, but a re-throw is useful to continue failing after doing things like logging of information or cleanup steps, perhaps even altering which error is thrown.

要做出错误显示为在Web控制台一个错误,因为你本来打算,我用这一招:

To make the error show up as an error in the web console, as you originally intended, I use this trick:

.catch(function(err) { setTimeout(function() { throw err; }); });

连行号生存,所​​以在Web控制台的链接带我直奔文件和行所在的(原)发生错误。

Even the line numbers survive, so the link in web console takes me straight to the file and line where the (original) error happened.

在一个称为一个承诺履行或拒绝处理函数的任何异常被自动转换为一个拒绝你应该回报的承诺。诺code调用你的函数完成这个功能。

Any exception in a function called as a promise fulfillment or rejection handler gets automatically converted to a rejection of the promise you're supposed to return. The promise code that calls your function takes care of this.

在另一方面通过所谓的setTimeout函数,总是从JavaScript稳定的状态下运行,即它运行在JavaScript事件循环一个新的周期。例外有没有抓到任何东西,并使其Web控制台。由于 ERR 保存所有有关错误的信息,包括原始堆栈,文件和行号,但它仍然被正确报告。

A function called by setTimeout on the other hand, always runs from JavaScript stable state, i.e. it runs in a new cycle in the JavaScript event loop. Exceptions there aren't caught by anything, and make it to the web console. Since err holds all the information about the error, including the original stack, file and line number, it still gets reported correctly.

这篇关于为什么我不能一个Promise.catch处理器中扔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆