Last active
February 14, 2017 10:30
-
-
Save vitalymak/13bedb6f63d4337439c4a87e82f9d205 to your computer and use it in GitHub Desktop.
Promise.all with wait all.
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
errors = []; | |
var promises = [ | |
Promise.reject(new Error('Rejects immediately!')), | |
new Promise((resolve) => setTimeout(resolve, 5000)).then(() => 'Resolved after 5 seconds!') | |
]; | |
promises = promises.map((promise, i) => promise.catch(error => { | |
errors[i] = error; | |
})); | |
Promise.all(promises) | |
.then(results => { | |
errors.forEach((error, i) => { | |
results[i] = error; | |
}); | |
results.forEach(res => { | |
console.log(res instanceof Error ? res.message : res); | |
}); | |
}); | |
// Prints after 5 seconds: | |
// Rejects immediately! | |
// Resolved after 5 seconds! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment