Skip to content

Instantly share code, notes, and snippets.

@Kannndev
Created October 4, 2020 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kannndev/f1c67749f8ba28ceb6edb5c2f27fe614 to your computer and use it in GitHub Desktop.
Save Kannndev/f1c67749f8ba28ceb6edb5c2f27fe614 to your computer and use it in GitHub Desktop.
Medium Promise.allSettled examples
// 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