Created
May 14, 2018 12:04
-
-
Save donaldpipowitch/c38d93e64a720431eae908336a8c1d6f to your computer and use it in GitHub Desktop.
async generator function: count until 5
This file contains 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
// works in current chrome (logs from 1 to 5 every second) | |
function sleep(milliseconds) { | |
return new Promise(resolve => setTimeout(resolve, milliseconds)) | |
} | |
async function* seconds() { | |
let second = 0; | |
while (true) { | |
await sleep(1000); | |
second += 1; | |
yield second; | |
} | |
} | |
async function example() { | |
for await (const second of seconds()) { | |
console.log(second); | |
if (second === 5) break; | |
} | |
} | |
example(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment