Skip to content

Instantly share code, notes, and snippets.

@DavidWells
Created August 13, 2020 19:14
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 DavidWells/7165247047d18a7faa80b36f2dbbc1f1 to your computer and use it in GitHub Desktop.
Save DavidWells/7165247047d18a7faa80b36f2dbbc1f1 to your computer and use it in GitHub Desktop.
var waitFor = (ms) => new Promise(r => setTimeout(r, ms))
async function asyncForEach(array, callback) { 
for (let index = 0; index < array.length; index++) {   
await callback(array[index], index, array) 
}
}
var start = async () => { 
let state = 0 
await asyncForEach([1, 2, 3], async (num) => {   
await waitFor(1000)   
console.log(num)   
state = state + num 
}) 
console.log('Done') 
return state
}
start().then((res) => { 
console.log(res)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment