Last active
February 21, 2022 05:23
-
-
Save ClementParis016/e392739820c0f2124a0b2076da5b15b7 to your computer and use it in GitHub Desktop.
Fault-tolerant Promise.all
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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