Skip to content

Instantly share code, notes, and snippets.

@donaldpipowitch
Created May 14, 2018 12:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donaldpipowitch/c38d93e64a720431eae908336a8c1d6f to your computer and use it in GitHub Desktop.
Save donaldpipowitch/c38d93e64a720431eae908336a8c1d6f to your computer and use it in GitHub Desktop.
async generator function: count until 5
// 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