Skip to content

Instantly share code, notes, and snippets.

@rayets123
Last active January 21, 2019 15:03
Show Gist options
  • Select an option

  • Save rayets123/a5ed8ca195250dc8220c46744ece1faf to your computer and use it in GitHub Desktop.

Select an option

Save rayets123/a5ed8ca195250dc8220c46744ece1faf to your computer and use it in GitHub Desktop.

Revisions

  1. rayets123 revised this gist Jan 21, 2019. 1 changed file with 2 additions and 4 deletions.
    6 changes: 2 additions & 4 deletions multiple promises in-parallel
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,14 @@
    async function bar() {
    await new Promise(r=> setTimeout(r, 1000))
    .then(()=> console.log('bar'))
    .then(()=> 'bar result');
    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))
    .then(()=> console.log('bat'))
    .then(()=> 'bat result');
    console.log('bat');
    }

    function handleRejection(p) {
  2. rayets123 created this gist Jan 21, 2019.
    25 changes: 25 additions & 0 deletions multiple promises in-parallel
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    async function bar() {
    await new Promise(r=> setTimeout(r, 1000))
    .then(()=> console.log('bar'))
    .then(()=> 'bar result');
    }
    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))
    .then(()=> console.log('bat'))
    .then(()=> 'bat result');
    }

    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));