Skip to content

Instantly share code, notes, and snippets.

@juliencrn
Created April 11, 2021 00:17
Show Gist options
  • Save juliencrn/e18f689c7d6c2815805d6b154d541800 to your computer and use it in GitHub Desktop.
Save juliencrn/e18f689c7d6c2815805d6b154d541800 to your computer and use it in GitHub Desktop.
// 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