捕获JavaScript中的Promises中的Promises中生成的错误 [英] Catching errors generated in Promises within Promises in JavaScript

查看:62
本文介绍了捕获JavaScript中的Promises中的Promises中生成的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可能在Promises中冒出错误?

Is it possible to have errors bubble up in Promises?

请参见下面的代码以供参考,我想获取promise1.catch来捕获promise2中生成的错误(当前不适用于此代码):

See the code below for reference, I'd like to get promise1.catch to catch the error generated in promise2 (which current does not work with this code):

function test() {
    var promise1 = new Promise(function(resolve1) {
        var promise2 = new Promise(function(resolve2) {
            throw new Error('Error in promise2!');
        });

        promise2.catch(function(error2) {
            console.log('Caught at error2', error2);
        });
    });

    promise1.catch(function(error1) {
        console.log('Caught at error1', error1);
    });
}

test();

推荐答案

是!

promise中的错误传播是其最强的防护之一.它的行为就像在同步代码中一样.

Yes!!

Error propagation in promises is one of its strongest suits. It acts exactly like in synchronous code.

try { 
    throw new Error("Hello");
} catch (e){
    console.log('Caught here, when you catch an error it is handled');
}

非常类似于:

Promise.try(function(){
    throw new Error("Hello");
}).catch(function(e){
    console.log('Caught here, when you catch an error it is handled');
});

就像在顺序代码中一样,如果您想对错误进行一些逻辑处理但又不将其标记为已处理,则可以将其重新抛出:

Just like in sequential code, if you want to do some logic to the error but not mark it as handled - you rethrow it:

try { 
   throw new Error("Hello");
} catch (e){
    console.log('Caught here, when you catch an error it is handled');
    throw e; // mark the code as in exceptional state
}

哪个会成为:

var p = Promise.try(function(){
   throw new Error("Hello");
}).catch(function(e){
    console.log('Caught here, when you catch an error it is handled');
    throw e; // mark the promise as still rejected, after handling it.
});

p.catch(function(e){
     // handle the exception
});

请注意,在Bluebird中,您可以输入类型和条件捕获,因此,如果您要做的只是在承诺的类型或内容上使用if来决定是否处理它-您可以保存它./p>

Note that in Bluebird you can have typed and conditional catches, so if all you're doing is an if on the type or contents of the promise to decide whether or not to handle it - you can save that.

var p = Promise.try(function(){
   throw new IOError("Hello");
}).catch(IOError, function(e){
    console.log('Only handle IOError, do not handle syntax errors');
});

您还可以使用.error来处理源自承诺API的OperationalError.通常,OperationalError表示您可以从中恢复的错误(与程序员错误相比).例如:

You can also use .error to handle OperationalErrors which originate in promisified APIs. In general OperationalError signifies an error you can recover from (vs a programmer error). For example:

var p = Promise.try(function(){
   throw new Promise.OperationalError("Lol");
}).error(function(e){
    console.log('Only handle operational errors');
});

这具有不使代码中的TypeError或语法错误静音的优势,这在JavaScript中可能很烦人.

This has the advantage of not silencing TypeErrors or syntax errors in your code, which can be annoying in JavaScript.

这篇关于捕获JavaScript中的Promises中的Promises中生成的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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