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*

code:

function* a() {
yield 1;
yield 2;
return "end"
}
//undefined
function* b() {
yield* a()
}
//undefined
c = b()
//b {<suspended>}

c.next()
//{value: 1, done: false}
c.next()
//{value: 2, done: false}
c.next()
//{value: undefined, done: true}
d = a()
//a {<suspended>}

d.next()
//{value: 1, done: false}
d.next()
//{value: 2, done: false}
d.next()
//{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);
}

...you 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!