Skip to content

Instantly share code, notes, and snippets.

@nybblr
Last active Jan 19, 2019
Embed
What would you like to do?
Tiny example of async generator functions
let timer = (ms) => new Promise(resolve => setTimeout(resolve, ms));
let producer = async function*() {
let counter = 0;
while (true) {
let delay = Math.random() * 1000;
await timer(delay);
yield counter++;
}
};
let consumer = async (source) => {
for await (let value of source) {
console.log(value);
}
};
consumer(producer());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment