something wrong about generator

# 郑宇光 (5 months ago)

generator function shouldn't allow a "return value" when use yield or it need to return {value: {value: "end", done: true}, done: true} at the iter end when use yield*


function* a() {
yield 1;
yield 2;
return "end"
function* b() {
yield* a()
c = b()
//b {<suspended>}
//{value: 1, done: false}
//{value: 2, done: false}
//{value: undefined, done: true}
d = a()
//a {<suspended>}
//{value: 1, done: false}
//{value: 2, done: false}
//{value: "end", done: true}
# Vic99999 (5 months ago)

or it need to return {value: {value: "end", done: true}, done: true}

this case is supported, seems, if to use:

function* b() { return yield* a() }

# Jeremy Martin (5 months ago)

The yield * expression actually evaluates to the final value of the delegated generator (i.e., the final value when done is true).

In other words, if you modified your second generator in your example to the following:

function* b() {
    const result = yield* a();
    console.log('THE RESULT', result);
} will see that result is equal to "end". So, to reiterate (pun not intended, but intentionally left intact), the final returned value of the delegated generator is not yielded from the parent generator, but is instead used as the value of the yield * expression itself.

Hope that helps!