Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 909
polkovnikov.ph Avatar asked Oct 30 '25 11:10

polkovnikov.ph


1 Answers

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

like image 85
Hrishi Avatar answered Nov 01 '25 03:11

Hrishi