To see how to call another generator inside a generator:
function* numbers () { yield 1; yield 2; yield* moreNumbers(); yield 6; yield 7; yield 8; } function* moreNumbers () { yield 3; yield 4; yield 5; } const generator = numbers(); const values = []; for (let val of generator) { values.push(val); } console.log(values); // [1,2,3,4,5,6,7,8]
Example:
class Tree { constructor (value, children= []) { this.value = value; this.children = children; } *printValues () { yield this.value; for (let child of this.children) { yield* child.printValues(); } } } const tree = new Tree(1, [ new Tree(2, [new Tree(4)]), new Tree(3) ]); const values = []; for (let value of tree.printValues()) { values.push(value) } console.log(values) // [1,2,4,3]