Resolution of yield return value in JavaScript gen

2019-05-28 09:25发布

问题:

If I supply a value to the next method on a generator, in addition to supplying an expression to the right of the yield keyword, how is the return value of the yield resolved?

function* go() {
  console.log(yield 'a');
  console.log(yield 'b');
}

var g = go();
g.next();
g.next('one');
g.next('two');

Output:

one
two
Object {value: undefined, done: true}

In light of the output, it is the value supplied to the next method that is used over the value returned by the expression to the right of yield.

Edit: code updated to better reflect my question.

回答1:

In light of the output, it is the value supplied to the next method that is used over the value returned by the expression to the right of yield.

The value supplied to the next method is not "used over" the value to the right of yield. The value supplied to next is what the yield expression ultimately evaluates to. Always. The value to the right of the yield is what is supplied to the consumer of the generator.

It is a two-way communication between the generator and the code consuming the generator.

function* go() {
  console.log(yield 'a');   // 'one'
  console.log(yield 'b');   // 'two'
}

var g = go();
console.log(g.next());       // { value: 'a', done: false }       
console.log(g.next('one'));  // { value: 'b', done: false }
console.log(g.next('two'));  // { value: undefined, done: true }


回答2:

There are two ways to interpret "return value of the yield".

You could mean the value of the expression yield 'a'. That has the value one, as you can see from your output.

Or you could mean the value passed to yield, which is effectively "returned" from the generator's next method. Your code snippet doesn't really reveal this. Try:

console.log('next1', g.next());
console.log('next2', g.next('one'));
console.log('next3', g.next('two'));

That way you can see the values coming out of the generator, which should look something like:

"next1", {value: "a", done: false}
"next2", {value: "b", done: false}
"next3", {value: undefined, done: true}

One other thing you can do is add an ordinary return value at the end of your generator function, and that will be the value for next3.