title: Bluebird nested promises with each/spread
body: <p>I'm facing an issue while using bluebird Promises. I'm using CoffeeScript but JavaScript answers are welcome too :)</p>
  
  <p>Here is what I'm trying to do :</p>
  
  <p><strong>Code example</strong></p>
  
  <pre><code>Promise = require 'bluebird'
  Model = Promise.promisifyAll(require '[...]') # mongoose model promisified
  
  getOpts = () -&gt; [...] # whatever
  
  Promise.each [1..3], (number) -&gt;
    opts = getOpts(number)
    return Model.count(opts).exec (err, count) -&gt;
      return "the count is #{count}"
  .spread () -&gt;
    console.log JSON.stringify arguments
    result = arguments.join(',')
    [...]
  </code></pre>
  
  <p><strong>Explanations</strong></p>
  
  <p>I want to run the same function with <code>1</code>, <code>2</code>, <code>3</code> (sequentially), so I use bluebird's <code>.each</code> function.
  In the function, I need to get a count from my database. I'm using <code>mongoose</code> with bluebird's <code>promisifyAll</code> function to return a Promise and make sure that <code>.each</code> waits until each query is done to go next.</p>
  
  <p>Then, I would like to gather the result of each query. I'm using bluebird's <code>spread</code> to gather the return values of <code>.each</code>. However, it doesn't contain the return value of the nested promise. <code>arguments</code> value is :</p>
  
  <pre><code>{
    "0": 1,
    "1": 2,
    "2": 3
  }
  </code></pre>
  
  <p>Any ideas ?</p>
  
  <p>Thanks</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Looks like bluebird's <code>.each</code> doesn't go well with <code>.spread</code>. I'm investigating on this issue.</p>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>(Thanks Bergi)</p>
  
  <p>I found a solution : first I build an array of functions. Then I call bluebird's <code>.all</code> on this array, and then <code>.spread</code>.</p>
  
  <p><strong>EDIT 3</strong></p>
  
  <p>Ok, I finally tried <code>.map</code> instead of <code>.each</code> and it works great also.</p>