4

I would like to call a generator from another generator getting its "steps". Though I cannot find a good syntax for that.

function* test1() {
    yield 2;
    yield 3;
}
function* test2() {
    yield 1;
    for (var i of test1()) yield i; // WTF
    yield 4;
}
var a = test2();
for (var b of a) {
    console.log(b);
}

Output: 1 2 3 4

How do I shorten that row?

polkovnikov.ph
  • 6,256
  • 6
  • 44
  • 79

1 Answers1

6

You could use the yield* syntax and replace the for.. of loop with just yield* test1()

Hrishi
  • 7,110
  • 5
  • 27
  • 26