Promise.all处理错误 [英] Error Handling with Promise.all

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

问题描述

在Promise.all的错误处理方面,我遇到了一个问题.

I'm facing an issue in regards to error handling with Promise.all

我希望以下代码在 getNetworkstuff()承诺之一失败时调用链的catch部分.但是相反,它只调用下一个然后是部分,浏览器控制台会显示未捕获的错误.

I would expect the following code to call the catch part of the chain when one of the getNetworkstuff() Promises fails. But instead it just calls the next then part and the Browser Console shows an uncaught Error.

Promise.all(
    [[getNetworkstuff(url1)],[getNetworkstuff(url2)]]
    //please note that the arrays within the array are larger in my application
    //otherwise I would just use one big array not arrays in arrays
)
.then(function(result){//Stuff worked})
.catch(function(err){//Stuff broke});

function getNetworkstuff(url){
    return new Promise(function(resolve,reject){//here will be awesome network code})
}

我可以看到诺言没有实现,因为返回的 result 数组包含适当的拒绝诺言.

I can see that the promise has not fulfilled as the returned result array contains the appropriate rejected promise.

[[PromiseStatus]]: "rejected"
[[PromiseValue]]: Error: HTTP GET resulted in HTTP status code 404.

有人可以告诉我为什么不调用 catch 吗?(我知道这是如果我在 Promise.all()中只是一个承诺数组,其中一个被拒绝)

Can somebody tell me why catch is not called? (I know it is if I just have an Array of Promises in Promise.all() of which one rejects)

推荐答案

查看控制台

function getNetworkstuff(url) {
    return new Promise(function(resolve, reject) {
        setTimeout(function() {
            console.log('resolving', url);
            resolve(url);
        }, 5000);
    });
}
Promise.all([[getNetworkstuff('url1')],[getNetworkstuff('url2')]])
.then(function(result){
    console.log('It worked', result);
})
.catch(function(err){
    console.log('It failed', result);
});

请注意,在解决任何问题之前5秒,它会输出它已工作"

Notice it outputs "It worked", 5 seconds BEFORE anything is resolved

Promise.all([getNetworkstuff('url1'), getNetworkstuff('url2')])
.then(function(result){
    console.log('It worked', result);
})
.catch(function(err){
    console.log('It failed', result);
});

现在不使用array数组进行比较-请注意在两种情况下 It Worked 旁边记录的内容不同

Now compare without the array of array - notice the difference in what is logged next to It Worked in both cases

最后,运行此

function getNetworkstuff(url) {
    return new Promise(function(resolve, reject) {
        if(url == 'url1') {
            setTimeout(function() {
                console.log('resolving', url);
                resolve(url);
            }, 5000);
        }
        else {
            console.log('rejecting', url);
            reject(url);
        }
    });
}

Promise.all([getNetworkstuff('url1'), getNetworkstuff('url2')])
.then(function(result){
    console.log('It worked', result);
})
.catch(function(err){
    console.log('It failed', result);
});

您的跟进问题:如果不被视为诺言,他们将如何踢退

您能看到下面的代码与您可能会或可能不会返回promise的函数结果数组的处理方式相似吗?无论如何,拿走承诺和随后的东西……而您已经得到了

Can you see that the code below is of a similar pattern to what you are doing with your array of array of results of functions that may or may not return promises? Anyway, take away the promises and the then stuff ... and you've got this

function fn(s) {
    return s.toUpperCase();
}
function fn2(arr) {
    console.log(arr); // [["A"], ["B"]]
}
fn2([[fn('a')],[fn('b')]]);

这篇关于Promise.all处理错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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