Created
November 27, 2016 12:30
-
-
Save sanketmeghani/12ef60e1dfd9d0696ac125789102e9c0 to your computer and use it in GitHub Desktop.
Example to delegate to another generator function using yield*
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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