Last active
June 3, 2020 14:34
-
-
Save bobdobbalina/892d36575d6f5111af0135cf970fdea2 to your computer and use it in GitHub Desktop.
Javascript: Wait until promises ar complete
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 PromiseArray = [ | |
Promise.resolve(100), | |
Promise.reject(null), | |
Promise.resolve("Data release"), | |
Promise.reject(new Error('Something went wrong'))]; | |
Promise.all(PromiseArray) | |
.then(data => console.log('all resolved! here are the resolve values:', data)) | |
.catch(err => console.log('got rejected! reason:', err)) | |
// If you want to wait until all your promises are complete, regardless whether they get rejected or resolved. | |
const PromiseArray = [ | |
Promise.resolve(100), | |
Promise.reject(null), | |
Promise.resolve("Data release"), | |
Promise.reject(new Error('Something went wrong'))]; | |
Promise.allSettled(PromiseArray) | |
.then(res => { | |
console.log(res); | |
}) | |
.catch(err => console.log(err)); | |
//[ | |
//{status: "fulfilled", value: 100}, | |
//{status: "rejected", reason: null}, | |
//{status: "fulfilled", value: "Data release"}, | |
//{status: "rejected", reason: Error: Something went wrong ...} | |
//] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment