Created
July 18, 2020 18:48
-
-
Save katepapineni/9cf16e299449283ab3f97a88b60f0db2 to your computer and use it in GitHub Desktop.
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
/* 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