I have a function that returns a function (a async .get call). Here I am returning a empty object in my catch statement:
export function makeHttpStatsProvider(fisClientRoot, httpClient, selectedDate = null) {
return async function(polygon, layerName) {
const response = await httpClient.get(
///request logic
}}).catch((e) => {
return {}
}).then((response) => {
return {[layerName]: response.data[layerName][0].basicStats}
})
}
}
Then I have a function that takes this promise and makes an array of promises:
export async function harvestMap(statsProvider, polygons, numBuckets = 5) {
const arrayOfPromises = polygons.map(async (polygon) => {
Object.assign({}, polygon, await statsProvider(polygon, 'EVI'))
})
return Promise.all(arrayOfPromises)
.catch((err) => {
console.log('a promise failed to resolve', err)
return arrayOfPromises
})
.then((arrayOfPromises) => {
return arrayOfPromises.sort((a, b) => a.EVI.mean - b.EVI.mean).map((polygon, index) => Object.assign({}, polygon, {
rank: Math.floor(index * numBuckets / polygons.length)
}))
})
}
Here the .catch statement console.log an error. But despite this, the arrayOfPromises in the .then() statement still includes rejected promises.
What I would like to achieve is to return a custom object when the network request fails, and pass it to the Object.assign function later on. Is this even possible or do I need to approach the problem in a different way?