简体   繁体   English

捕获JavaScript中的Promises中的Promises中生成的错误

[英]Catching errors generated in Promises within Promises in JavaScript

Is it possible to have errors bubble up in Promises? 是否有可能在Promises中出现错误?

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): 请参阅下面的参考代码,我想获得promise1.catch赶上产生的误差promise2 (电流不使用此代码工作):

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();

Yes!! 是!!

Error propagation in promises is one of its strongest suits. promises中的错误传播是其最强大的诉讼之一。 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');
}

Is very similar to: 非常相似:

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
}

Which becomes: 哪个成了:

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
});

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. 请注意,在蓝鸟您可以键入和有条件的渔获量,因此,如果你正在做的是if类型或承诺的内容,以决定是否要处理它-你可以保存。

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

You can also use .error to handle OperationalError s which originate in promisified APIs. 您还可以使用.error来处理源自promisified API的OperationalError In general OperationalError signifies an error you can recover from (vs a programmer error). 通常, OperationalError表示可以从中恢复的错误(与程序员错误相比)。 For example: 例如:

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

This has the advantage of not silencing TypeError s or syntax errors in your code, which can be annoying in JavaScript. 这样做的好处是不会在代码中沉默TypeError或语法错误,这在JavaScript中可能很烦人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM