I have the following code
await doAllCats();
await doAllDogs();
console.log("Finished processing Cats and Dogs ")
function doAllCats() {
let promiseArray = [];
for(let cat of cats) {
promiseArray.push(doOneCat(cat));
}
return Promise.all(promiseArray);
}
function doOneCat(cat) {
let promise = doSomeAsyncStuffWithACat(cat);
promise.catch((err)=> {
console.error("there was a problem with "+cat+" but processing should continue as normal for the other cats and dogs");
})
return promise;
}
which is working fine when all Cats and Dogs succeed. However, sometimes a cat will fail, and when that happens, I get an exception at the topmost level. The code that deals with a cat failing is inside doOneCat
and that is performing correctly. However, the failed promise is still in the promiseArray
and so I get a premature "finished".
What is the simplest/canonical method of preventing Promise.all
rejecting on the first exception?