Created
October 4, 2020 10:29
-
-
Save Kannndev/f1c67749f8ba28ceb6edb5c2f27fe614 to your computer and use it in GitHub Desktop.
Medium Promise.allSettled examples
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
// Example 1: | |
const dog = new Promise((resolve, reject) => { | |
setTimeout(() => resolve('🐶'), 1000) | |
}) | |
const cat = new Promise((resolve, reject) => { | |
setTimeout(() => reject('🐈'), 2000) | |
}) | |
Promise.allSettled([dog, cat]).then((values) => { | |
console.log(values); | |
// [{ status: 'fulfilled', value: '🐶' }, | |
// { status: 'rejected', // reason: '🐈' }] | |
}); | |
// Practical Usage: | |
// I have mostly used this for batch processing where | |
// we identify the failed ones and retry separately. | |
Promise.allSettled([ | |
fetch('/endpoint0'), | |
fetch('/endpoint1'), | |
}) | |
]).then(response => console.log(response)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment