Skip to content

Instantly share code, notes, and snippets.

@vitalymak
Last active February 14, 2017 10:30
Show Gist options
  • Save vitalymak/13bedb6f63d4337439c4a87e82f9d205 to your computer and use it in GitHub Desktop.
Save vitalymak/13bedb6f63d4337439c4a87e82f9d205 to your computer and use it in GitHub Desktop.
Promise.all with wait all.
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