Skip to content

Instantly share code, notes, and snippets.

@nybblr
Last active January 19, 2019 02:16
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 nybblr/6600a7d2fbd9e36fc09ae9ddd10a0b02 to your computer and use it in GitHub Desktop.
Save nybblr/6600a7d2fbd9e36fc09ae9ddd10a0b02 to your computer and use it in GitHub Desktop.
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