Created
June 25, 2018 22:45
-
-
Save fxfactorial/4f3df3c8836576e88c56ab0b9cb3473b to your computer and use it in GitHub Desktop.
simple async generators
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
async function* handle() { | |
let i = 0; | |
while (true) { | |
yield i++; | |
} | |
} | |
const sleep = () => new Promise(r => setTimeout(() => r(undefined), 1000)); | |
async function main() { | |
const results = handle(); | |
// Imagine this with WebSockets | |
for await (const h of results) { | |
console.log({ h }); | |
await sleep(); | |
} | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment