I am fetching URLs specified in the array and then combine fetched results. I want to ignore failed fetches.
While are tons of post on the subject:
- Wait until all promises complete even if some rejected
- https://gist.github.com/nhagen/a1d36b39977822c224b8
I just can't figure our how to apply this to my code, that fetches URLs from the array:
Promise.all (arrayOfBlobs.map (x => fetch (x).then (response => response.json())) )
.then (json => {
json.forEach ( x => {
if (Array.isArray (x)) {
// this json has array of objects
console.log (`Received ${x.length} prospects`)
x.forEach ( y => combinedArray.push (y) )
}
else {
// this json has single prospect object
console.log (`Received single prospect`)
combinedArray.push (x)
}
})
this.setState({loadingTable: false, data: combinedArray})
})
.catch (error => {
console.error (error.message)
this.setState({loadingTable: false, data: combinedArray})
})
For example below did not work:
Promise.all (arrayOfBlobs.map (x => fetch (x).then (response => response.json())) )
.then (json => {
json.forEach ( x => {
if (Array.isArray (x)) {
// this json has array of objects
console.log (`Received ${x.length} prospects`)
x.forEach ( y => combinedArray.push (y) )
}
else {
// this json has single prospect object
console.log (`Received single prospect`)
combinedArray.push (x)
}
})
.catch (e => {console.log (`Failed to fetch due to ${e.message}`)})
this.setState({loadingTable: false, data: combinedArray})
})
.catch (error => {
console.error (error.message)
this.setState({loadingTable: false, data: combinedArray})
})
What do I need to do to modified my code so failed fetches are ignored?