title: How to chain a variable number of promises in Q, in order?
body: <blockquote>
    <p>I have seen <a href="https://stackoverflow.com/questions/16976573/chaining-an-arbitrary-number-of-promises-in-q">Chaining an arbitrary number of promises in Q</a> ; my question is different. </p>
  </blockquote>
  
  <p>How can I make a variable number of calls, each of which returns asynchronously, in order?<br>
  The scenario is a set of HTTP requests, the number and type of which is determined by the results of the first HTTP request.</p>
  
  <p>I'd like to do this simply. </p>
  
  <p>I have also seen <a href="https://stackoverflow.com/a/17452570/48082%20this%20answer">this answer</a> which suggests something like this: </p>
  
  <pre><code>var q = require('q'),
      itemsToProcess =  ["one", "two", "three", "four", "five"];
  
  function getDeferredResult(prevResult) {
    return (function (someResult) {
      var deferred = q.defer();
      // any async function (setTimeout for now will do, $.ajax() later)
      setTimeout(function () {
        var nextResult = (someResult || "Initial_Blank_Value ") + ".." + itemsToProcess[0];
        itemsToProcess = itemsToProcess.splice(1);
        console.log("tick", nextResult, "Array:", itemsToProcess);
        deferred.resolve(nextResult);
      }, 600);
  
      return deferred.promise;
    }(prevResult));
  }
  
  var chain = q.resolve("start");
  for (var i = itemsToProcess.length; i &gt; 0; i--) {
      chain = chain.then(getDeferredResult);
  }
  </code></pre>
  
  <p>...but it seems awkward to loop through the itemsToProcess in that way.  Or to define a new function called "loop" that abstracts the recursion.  What's a better way?</p>