title: forEach using generators in Node.js
body: <p>I'm using <a href="http://koajs.com">Koa.js</a> framework and <a href="http://mongoosejs.com/">Mongoose.js</a> module.</p>
  
  <p>Normally to get a result from MongoDB I code like this:</p>
  
  <pre><code>var res = yield db.collection.findOne({id: 'my-id-here'}).exec();
  </code></pre>
  
  <p>But I need to execute this line for every element of an array named 'items'.</p>
  
  <pre><code>items.forEach(function(item) {
    var res = yield db.collection.findOne({id: item.id}).exec();
    console.log(res)  // undefined
  });
  </code></pre>
  
  <p>But this code doesn't run as yield is in the function. If I write this:</p>
  
  <pre><code>items.forEach(function *(item) {
    var res = yield db.collection.findOne({id: item.id}).exec();
    console.log(res)  // undefined
  });
  </code></pre>
  
  <p>I'm not getting the result in res variable either. I tried to use '<a href="https://www.npmjs.org/package/generator-foreach">generator-foreach</a>' module but that didn't worked like this.</p>
  
  <p>I know that this is my lack of knowledge about the language literacy of Node.js. But can you guys help me finding a way how to do this?</p>