title: How to run a promise-then chain using map or reduce on an arbitrary number of chain elements?
body: <p>I'm stuck on the following:</p>
  
  <p>A script is returning an arbitrary number <code>n</code> or array, like this:</p>
  
  <pre><code>[["a"], ["b"], ["c"], ["d"]]
  </code></pre>
  
  <p>I need to loop over the arrays using promise <code>then()</code>, but as I don't know how many elements there will be, I have ended up doing this:</p>
  
  <pre><code>  var bundle_list = [["a"], ["b"], ["c"], ["d"]];
  
    var x = bundle_list.reduce(function(current, next) {
    console.log(current);
  
    // requestBundle will also return a promise
    return requestBundle(current)
      .then(function(bundle_response) {
        // do foo
        console.log("CALLING NEXT")
        console.log(next);
        return RSVP.resolve(next);
      });
  })
  
  x.then(function(last_response) {
    return console.log("DONE")
  });
  </code></pre>
  
  <p>My problem is that my <code>reduce/map</code> both trigger all iterations before my async code is run, so I'm getting 3x the <code>current</code> console followed by the <code>done</code> console. So all my map "loop" run instantly, with the results clocking in a little (too) later...</p>
  
  <p>I'm using this <a href="https://github.com/tildeio/rsvp.js" rel="noreferrer">RSVP</a> implementation, but it's A+ so should not be a problem. I have been trying to work along the answer provided <a href="https://stackoverflow.com/questions/17757654/how-to-chain-a-variable-number-of-promises-in-q-in-order">here</a>, but I cannot get it to work properly.</p>
  
  <p><strong>Question:</strong><br>
  Is it possible to create a "then-chain" with an arbitrary number of <code>then</code> statements. If so, some pointers are appreciated!</p>
  
  <p>Thanks!</p>