Skip to content

Instantly share code, notes, and snippets.

@sanketmeghani
Created November 27, 2016 12:30
Show Gist options
  • Save sanketmeghani/12ef60e1dfd9d0696ac125789102e9c0 to your computer and use it in GitHub Desktop.
Save sanketmeghani/12ef60e1dfd9d0696ac125789102e9c0 to your computer and use it in GitHub Desktop.
Example to delegate to another generator function using yield*
function* secondGenerator(a) {
yield (a + 1);
yield (a + 2);
yield (a + 3);
}
function* firstGenerator(a) {
yield a;
yield* secondGenerator(a);
yield (a + 10);
}
let generator = firstGenerator(10);
console.log(generator.next().value); // 10 - {value: 10, done: false}
console.log(generator.next().value); // 11 - {value: 11, done: false}
console.log(generator.next().value); // 12 - {value: 12, done: false}
console.log(generator.next().value); // 13 - {value: 13, done: false}
console.log(generator.next().value); // 20 - {value: 20, done: true}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment