Skip to content

Instantly share code, notes, and snippets.

@rayets123
Last active January 21, 2019 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rayets123/a5ed8ca195250dc8220c46744ece1faf to your computer and use it in GitHub Desktop.
Save rayets123/a5ed8ca195250dc8220c46744ece1faf to your computer and use it in GitHub Desktop.
await multiple promises in-parallel without 'fail-fast' behavior
async function bar() {
await new Promise(r=> setTimeout(r, 1000))
console.log('bar');
}
async function bam() {
await new Promise((ignore, reject)=> setTimeout(reject, 2000))
.catch(()=> { console.log('bam errored'); throw 'bam'; });
}
async function bat() {
await new Promise(r=> setTimeout(r, 3000))
console.log('bat');
}
function handleRejection(p) {
return p.catch(err=> ({ error: err }));
}
async function foo(arr) {
console.log('foo');
return await Promise.all([bar(), bam(), bat()].map(handleRejection));
}
foo().then(results=> console.log('done', results));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment