Created
April 11, 2021 00:17
-
-
Save juliencrn/e18f689c7d6c2815805d6b154d541800 to your computer and use it in GitHub Desktop.
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
// ES6 Generators | |
// @See: https://medium.com/dailyjs/a-simple-guide-to-understanding-javascript-es6-generators-d1c350551950 | |
function* gChild() { | |
yield 2; | |
yield 3; | |
} | |
function* gMain() { | |
yield 1; | |
yield* gChild(); | |
yield 4; | |
} | |
let generator = gMain(); | |
// console.log(generator.next()); // { value: 1, done: false } | |
// console.log(generator.next()); // { value: 2, done: false } | |
// console.log(generator.next()); // { value: 3, done: false } | |
// console.log(generator.next()); // { value: 4, done: false } | |
// console.log(generator.next()); // { value: undefined, done: true } | |
// or | |
for (const iterator of generator) { | |
console.log(iterator); // 1, 2, 3, 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment