Created
January 10, 2019 17:00
-
-
Save quidmonkey/cabeb9dc19b745bd33c777033592136a to your computer and use it in GitHub Desktop.
Async Loop Example
This file contains hidden or 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
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