Is revenue * equivalent to a while loop with equal output in javascript?

If I have a line like:

yield* foo() 

Can I replace it with something with a melody:

 while(true) { var x = foo.next(); if(x.done) break; yield x; } 

Clearly this is more verbose, but I'm trying to figure out if yield * is just syntactic sugar, or if there is some kind of semantic aspect that I don't understand about.

+5
source share
1 answer

Instead of yield x you need to do yield x.value . You also need to call foo to get the iterator. .next is the iterator method returned by the foo generator.

 function *foo() { yield 1; yield 2; yield 3; } function *f() { yield *foo(); } console.log(Array.from(f())); function *g() { var iterator = foo(); while (true) { var x = iterator.next(); if (x.done) break; yield x.value; } } console.log(Array.from(g())); 
+1
source

Source: https://habr.com/ru/post/1262433/


All Articles