【发布时间】: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 归类为可迭代的,不是吗?