Skip to content

Instantly share code, notes, and snippets.

@quidmonkey
Created January 10, 2019 17:00
Show Gist options
  • Save quidmonkey/cabeb9dc19b745bd33c777033592136a to your computer and use it in GitHub Desktop.
Save quidmonkey/cabeb9dc19b745bd33c777033592136a to your computer and use it in GitHub Desktop.
Async Loop Example
const sleep = async ms => new Promise(
resolve => setTimeout(() => {
console.log('~~~ Slept for', ms, 'ms')
resolve()
}, ms)
)
const of = async range => {
for (const ms of range) {
await sleep(ms)
}
}
const map = async range => {
await range.map(async ms => sleep(ms))
}
const main = async () => {
const range = [...Array(5).keys()].map(val => val * 500)
await of(range)
await map(range)
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment