Skip to content

Instantly share code, notes, and snippets.

@matjahs
Created January 2, 2022 13:26
Show Gist options
  • Save matjahs/18f9fa503b36370fe63a4f649db08dae to your computer and use it in GitHub Desktop.
Save matjahs/18f9fa503b36370fe63a4f649db08dae to your computer and use it in GitHub Desktop.
Async Generators
async function * generateInput() {
yield 1;
yield 2;
yield 3;
}
async function * addOne (input) {
for await (const value of input) {
yield value + 1;
}
}
async function * timesTwo(input) {
for await (const value of input) {
yield value * 2;
}
}
async function main() {
const input = generateInput();
for await (const n of addOne(timesTwo(input))) {
console.log(n);
}
}
main().catch(console.error);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment