Skip to content

Instantly share code, notes, and snippets.

@ClementParis016
Last active February 21, 2022 05:23
Show Gist options
  • Save ClementParis016/e392739820c0f2124a0b2076da5b15b7 to your computer and use it in GitHub Desktop.
Save ClementParis016/e392739820c0f2124a0b2076da5b15b7 to your computer and use it in GitHub Desktop.
Fault-tolerant Promise.all
const p1 = new Promise(resolve => resolve({ data: 1, status: 'OK' }));
const p2 = new Promise((resolve, reject) => reject({ data: 2, status: 'OK' }));
const p3 = new Promise(resolve => resolve({ data: 3, status: 'OK' }));
const promisesCatcher = promises => promises.map(promise => promise.catch(err => ({ data: err, status: 'ERROR' })));
Promise.all(
promisesCatcher([p1, p2, p3])
).then(values => {
for (let result of values) {
if (result.status === 'ERROR') {
console.error(result.data)
} else {
console.log(result.data);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment