2

Can someone help me understand what the following code is doing? Why is "one" not printed to the console? Also, is g the generator or is go the generator?

function *go() {
  var foo = yield;
  console.log(foo);
}

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

Output:

Object {value: undefined, done: false}
two
Object {value: undefined, done: true}
2
  • @Juhana - why did you put the run snippet functionality here? Commented Apr 2, 2015 at 20:40
  • 1
    ...so that people can try it for themselves? That's what it's for. Commented Apr 2, 2015 at 20:46

3 Answers 3

3

Why is "one" not printed to the console?

Because the first .next() invocation (that typically gets no arguments) does only advance the generator from the begin of the functin body code to the first yield expression (and returns the value that was yielded there). The value that was "passed in" is not accessible anywhere.

Only the second .next(…) call then supplies the value that comes out of the yield expression, continuing the generator state unto the next yield or the end of the function.

Also, is g the generator or is go the generator?

g is the generator (which is also an iterator). go is a generator function that creates generators (like a constructor creates instances).

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. And the value passed into next invocations becomes the value returned by the corresponding yield statement.
1

Just to add to the discussion. Here is a demonstration showing what I think you wanted to achieve. To quote from the Mozilla docs:

Calling a generator function does not execute its body immediately; an iterator object for the function is returned instead. When the iterator's next() method is called, the generator function's body is executed until the first yield expression, which specifies the value to be returned from the iterator or, with yield*, delegates to another generator function. The next() method returns an object with a value property containing the yielded value and a done property which indicates whether the generator has yielded its last value.

function* go(values) {
  while (values.length) {
    yield values.shift();
  }
}

var g = go(['one', 'two']);
document.getElementById("output").innerHTML += JSON.stringify(g.next());
document.getElementById("output").innerHTML += JSON.stringify(g.next());
// I'm done!
document.getElementById("output").innerHTML += JSON.stringify(g.next());
<pre id="output"></pre>

Comments

0

The first line is the object result of yield. The value is undefined because you have nothing after yield keyword but the function is not done.

The two is printed by the log function in the function.

The third line is the result of the function ending the value is still undefined (no return statement or value) and the function is done.

Comments

Your Answer

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.