Skip to content

Instantly share code, notes, and snippets.

@fxfactorial
Created June 25, 2018 22:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fxfactorial/4f3df3c8836576e88c56ab0b9cb3473b to your computer and use it in GitHub Desktop.
Save fxfactorial/4f3df3c8836576e88c56ab0b9cb3473b to your computer and use it in GitHub Desktop.
simple async generators
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