Skip to content

Instantly share code, notes, and snippets.

@adriancbo
Forked from atinux/async-foreach.js
Created May 20, 2019 02:46
Show Gist options
  • Save adriancbo/f0139fbcb2a1c7bf21a37519b80fe3c5 to your computer and use it in GitHub Desktop.
Save adriancbo/f0139fbcb2a1c7bf21a37519b80fe3c5 to your computer and use it in GitHub Desktop.

Revisions

  1. @atinux atinux created this gist Oct 5, 2017.
    16 changes: 16 additions & 0 deletions async-foreach.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    const waitFor = (ms) => new Promise(r => setTimeout(r, ms))
    const asyncForEach = (array, callback) => {
    for (let index = 0; index < array.length; index++) {
    await callback(array[index], index, array)
    }
    }

    const start = async () => {
    await asyncForEach([1, 2, 3], async (num) => {
    await waitFor(50)
    console.log(num)
    })
    console.log('Done')
    }

    start()