Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.