Skip to content

Instantly share code, notes, and snippets.

@katepapineni
Created July 18, 2020 18:48
Show Gist options
  • Save katepapineni/9cf16e299449283ab3f97a88b60f0db2 to your computer and use it in GitHub Desktop.
Save katepapineni/9cf16e299449283ab3f97a88b60f0db2 to your computer and use it in GitHub Desktop.
/* Promise.all() => Wait for multiple promises to resolve then proceed */
const promise1 = new Promise((resolve, reject) => {
setTimeout(resolve, 1000, 'Resolving 1');
});
const promise2 = new Promise((resolve, reject) => {
setTimeout(resolve, 2000, 'Resolving 2');
});
const promise3 = new Promise((resolve, reject) => {
setTimeout(resolve, 1000, 'Resolving 3');
});
// Pass Promise.all an array of promises
Promise.all([
promise1,
promise2,
promise3,
]).then((response) => {
console.log(response); // Expecting back an array of promise responses
}).catch((error) => {
console.log(`If any promise rejects error is caught here: ${error}`);
});
// => Output: [ 'Resolving 1', 'Resolving 2', 'Resolving 3' ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment