Given an array of Promises, and a Promise.all() statement, if one of the promises fails, the entire chain stops (it doesn't wait for any subsequent Promises to fulfill) and Promise.all().catch() fires, but Promise.all().then() does not.
However, if the individual Promises have their own 'catch' blocks, and if whatever problem caused the Promise to fail can be fixed within that catch block, how can we prevent the rest of the promises from being ignored/failing, so that Promises.all().then() will still execute? Is there some way to "negate" the error from within the .catch() block, so Promises.all() doesn't see it? I tried return Promise.resolve()
at the end of the .catch() block, but it doesn't work.
Example:
let promise_array = [];
let p = somePromisyFunction1()
.then(() => { // Do something good })
.catch(() => { // Handle the bad });
promise_array.push(p);
p = somePromisyFunction2()
.then(() => { // Do something good })
.catch(() => { // Handle the bad });
promise_array.push(p);
p = somePromisyFunction3()
.then(() => { // Do something good })
.catch(() => { // Handle the bad });
promise_array.push(p);
// ...
// More Promisy functions added to promise_array here...
// ...
Promise.all(promise_array)
.then(() => { // We're all done! All promises fulfilled. })
.catch(() => { // Something went wrong! });
In this example, if any one of the somePromisyFunctionX() fails, that function's .catch() clause will execute, and then the Promise.all.catch() clause will also immediately execute, without waiting for any of the subsequent promises to fulfill.
However, if we can fix whatever bad thing happened, within the individual function's .catch() clause, how can we prevent Promises.all().catch() from being immediately triggered, and instead allowing that the rest of the promises can go ahead and fulfill? In some way, in the .catch() clause, can we "negate" the error, so it looks like the promise actually fulfilled successfully instead of appearing to fail?
I can do this in an async/await function using try/catch blocks, but not sure how to do it with .then/.catch.