title: CoffeeScript: Using yield with recursion
body: <p>I'm trying to understand how <code>yield</code> works with recursion. For example, we have the following function in CoffeeScript that computes the factorial of a positive integer and returns the current cumulative product:</p>
  
  <pre><code>prod = 1
  f = (n) -&gt;
    if n &gt; 0
      prod = n * prod
      yield prod
      f(n-1)
  </code></pre>
  
  <p>So if we call this function with <code>a = f(3)</code> and stepping through <code>a.next()</code>, I expect to see something like</p>
  
  <pre><code>{value: 3, done: false} // prod = 3 * 1
  {value: 6, done: false} // prod = 3 * 2
  {value: 6, done: true}  // prod = 3 * 2 * 1
  </code></pre>
  
  <p>However, the actual output is</p>
  
  <pre><code>{value: 3, done: false}
  {value: {}, done: false}
  {value: undefined, done: true}
  {value: undefined, done: true}
  ...
  </code></pre>
  
  <p>Could anyone explain what's going on here? And how I should change my code to get the desired results? Thanks!</p>