title: Trying to understand generators / yield in node.js - what executes the asynchronous function?
body: <p>Node.js now has generators.</p>
  
  <p>My understanding is that generators can be used to write code that appears to be much more linear and avoids callback hell and pyramid of doom style coding.</p>
  
  <p>So to this point, my understanding is that inside a generator, code executes until it reaches a "yield" statement. Execution of the generator function suspends at this point. The <code>yield</code> statement specifies a return value which may be a function. Typically this would be a blocking I/O function - one that would normally need to be executed asynchronously.</p>
  
  <p>The yield's return function is returned to whatever called the generator.</p>
  
  <p>My question is, what happens at this point? What exactly executes the blocking I/O function that the yield returned?</p>
  
  <p>Is it correct that to write generator/yield code that appears to be linear, there needs to be a specific sort of function that is calling the generator, a function that loops through the generator and executes each asynch function returned by the yield and returns the result of the asynch function back into the generator?</p>
  
  <p>It's still not clear to me exactly how the asynch function returned by the yield is executed.  If it is executed by the function that calls the generator, is it executed asynchronously? I'm guessing so because to do otherwise would result in blocking behaviour.</p>
  
  <p>To summarise my questions:</p>
  
  <ol>
  <li>To write "linear" asynch code with generators, is it necessary for there to be a calling function that iterates over the generator, executing yielded functions as callbacks and returning the result of the callback back into the generator?</li>
  <li>If the answer to question 1 is yes, the exactly how are the yielded functions executed - asynchronously?</li>
  </ol>
  
  <p>Can anyone offer a better overview/summary of how the whole process works?</p>