admin管理员组文章数量:1399013
If I have multiple fetch()
calls within a Promise.all
block, when one fails, it all fails. That's cool, I need them all to resolve.
However, how can I find which one actually failed?
In the following code, the catch error
tells me:
TypeError: Failed to fetch`
which makes it impossible to construct an effective user error message should I choose to do so.
const goodUrl = '.1.3/jquery.min.js';
const badUrl = '.1.3/jquery.min.js';
Promise.all([
fetch(goodUrl),
fetch(badUrl), /* will fail */
fetch(goodUrl)
]).then(([response1, response2, response3]) => {
console.log(response1);
console.log(response2);
console.log(response3);
}).catch((err) => {
console.log(err);
});
Here's a fiddle (a snippet didn't play nice)
I have looked for duplicates, this is not one as the examples throw the same error
If I have multiple fetch()
calls within a Promise.all
block, when one fails, it all fails. That's cool, I need them all to resolve.
However, how can I find which one actually failed?
In the following code, the catch error
tells me:
TypeError: Failed to fetch`
which makes it impossible to construct an effective user error message should I choose to do so.
const goodUrl = 'https://ajax.googleapis./ajax/libs/jquery/2.1.3/jquery.min.js';
const badUrl = 'https://ajax.googleapis77./ajax/libs/jquery/2.1.3/jquery.min.js';
Promise.all([
fetch(goodUrl),
fetch(badUrl), /* will fail */
fetch(goodUrl)
]).then(([response1, response2, response3]) => {
console.log(response1);
console.log(response2);
console.log(response3);
}).catch((err) => {
console.log(err);
});
Here's a fiddle (a snippet didn't play nice)
I have looked for duplicates, this is not one as the examples throw the same error
Share Improve this question asked Nov 11, 2018 at 19:49 StudioTimeStudioTime 24.1k40 gold badges128 silver badges215 bronze badges2 Answers
Reset to default 6You could just catch each of them individually and throw
some custom errors like:
...
Promise.all([
fetch(goodUrl).catch(err => {
throw {url: goodUrl, err};
}),
fetch(badUrl).catch(err => {
throw {url: badUrl, err};
}),
fetch(goodUrl).catch(err => {
throw {url: goodUrl, err};
})
])
...
I'd suggest you make your own little wrapper around fetch()
that throws a custom error that contains the desired information:
function myFetch(url) {
return fetch(url).catch(function(err) {
let myError = new Error("Fetch operation failed on " + url);
myError.url = url;
throw myError;
});
}
const goodUrl = 'https://ajax.googleapis./ajax/libs/jquery/2.1.3/jquery.min.js';
const badUrl = 'https://ajax.googleapis77./ajax/libs/jquery/2.1.3/jquery.min.js';
Promise.all([
myFetch(goodUrl),
myFetch(badUrl), /* will fail */
myFetch(goodUrl)
]).then(([response1, response2, response3]) => {
console.log(response1);
console.log(response2);
console.log(response3);
}).catch((err) => {
console.log(err);
});
Working jsFiddle demo: https://jsfiddle/jfriend00/1q34ah0g/
本文标签: javascriptMultiple fetch() in Promiseallwhich one failedStack Overflow
版权声明:本文标题:javascript - Multiple fetch() in Promise.all - which one failed? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744175611a2593983.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论