【问题标题】:Error Handling with Promise.allPromise.all 的错误处理
【发布时间】:2015-08-18 12:46:12
【问题描述】:

我遇到了一个关于 Promise.all 错误处理的问题

我希望以下代码在getNetworkstuff() Promises 之一失败时调用链的捕获部分。但它只是调用下一个 then 部分,并且浏览器控制台显示未捕获的错误。

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 数组包含适当的拒绝承诺。

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

谁能告诉我为什么不调用catch? (我知道如果我在 Promise.all() 中有一个 Promise 数组,其中一个拒绝)

【问题讨论】:

  • 你有一个数组数组。您很可能需要致电Promise.all
  • Promise.all 将一组承诺作为它的参数,而不是一组数组
  • 实际上,我尝试了数组数组,它​​确实可以尽职尽责地工作
  • 是的,正如大家所说,首先要尝试删除其中一个数组级别: Promise.all( [ getNetworkstuff(url1), getNetworkstuff(url2) ], ...跨度>
  • 我很想摆脱一层数组,但我有大约 40 个 URL,我想从中加载数据。因此,我在各自的数组中有相关信息。 MDN Page 表示参数必须是“可迭代的,例如数组”。 Array 中的 Array 归类为可迭代的,不是吗?

标签: javascript es6-promise


【解决方案1】:

看看你的控制台

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 秒输出“它有效”

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

现在比较不使用数组的数组 - 注意两种情况下It Worked 旁边记录的内容的差异

最后,运行这个

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

您的后续问题:how are they kicked of if not being recognized as promises

您是否看到下面的代码与您对可能返回或不返回 Promise 的函数结果数组所做的操作具有相似的模式?无论如何,拿走承诺和其他东西......你就得到了这个

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

【讨论】:

  • 如果您不介意,只是一个快速跟进问题。如果 Promise.all 不将数组的内容识别为在调用 then 之前等待的承诺,如果不被识别为承诺,它们如何被踢出?
  • 感谢您回答我的问题。我已阅读 Mozilla 页面,但没有找到后续问题的答案,但我很感谢您有时间回答我的两个问题。
  • 我在答案中添加了更多信息 - 我想我知道您可能会在后续问题中问什么,并且 MDN 页面可能没有解释所有这些(MDN 页面解释了什么如果您将非 Promise 传递给 Promise.all,则会发生这种情况)
  • 好的,我想我有个主意了,谢谢。我得再考虑一下,然后也许我会完全掌握这个问题。与此同时,我将重写我的数组。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-12-20
  • 2020-09-19
  • 2017-09-19
  • 2019-10-13
  • 2020-09-04
  • 2021-08-21
  • 2016-08-18
相关资源
最近更新 更多