Last active
January 21, 2019 15:03
-
-
Save rayets123/a5ed8ca195250dc8220c46744ece1faf to your computer and use it in GitHub Desktop.
await multiple promises in-parallel without 'fail-fast' behavior
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
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