Search code examples
javascriptgeneratoryieldecmascript-harmony

Call a generator from generator


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?


Solution

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