2

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.

asked Apr 2, 2015 at 21:32
4
  • What do you mean by "resolved"? Commented Apr 2, 2015 at 21:40
  • Notice that you can simplify your example to function* go() { console.log(yield 'a'); } Commented Apr 2, 2015 at 21:42
  • It seems like there is conflict between the values that could be returned by the yield - either the value passed as an argument to next, or the value returned by the expression to the right of the yield keyword. What are the rules for determining which one is "returned" by the yield. Commented Apr 2, 2015 at 21:44
  • 1
    The value of the right is never returned from the yield expression, except it's driven like g.next(g.next().value) Commented Apr 2, 2015 at 22:10

2 Answers 2

3

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 }

answered Apr 2, 2015 at 21:58
Sign up to request clarification or add additional context in comments.

1 Comment

+1 for pointing out It is a two-way communication between the generator and the code consuming the generator. And I guess the explanation provided by MDN about the rv is a bit misleading since "the value of expression" may refer to the expression to the right of yield.
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.

answered Apr 2, 2015 at 21:54

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.