title: Chaining an arbitrary number of promises in Q
body: <p>I want to send an HTTP request N times. I want to eventually have information about the results of each of those requests.</p>
  
  <p>Running the request function once works great. Here's the HTTP request function using Q.defer():</p>
  
  <pre><code>function runRequest() {
      var deferred = Q.defer(),
          start = (new Date).getTime(),
          req = HTTP.request(options, function(res) {
              var end = (new Date).getTime(),
              requestDetails = {
                  reqStatus: res.statusCode,
                  reqStart: start,
                  reqEnd: end,
                  duration: end - start
              }
              deferred.resolve(requestDetails);
          });
      req.on('error', function(e) {
          deferred.reject(e.message);
      });
      req.end();
  
      return deferred.promise;
  }
  </code></pre>
  
  <p>If I do this, I get back the data I expect:</p>
  
  <pre><code>runRequest().then(function(requestDetails) {
      console.log('STATUS: ' + requestDetails.reqStatus);
      console.log('Duration: ' + requestDetails.duration);
      console.log('Start: ' + requestDetails.reqStart);
      console.log('End: ' + requestDetails.reqEnd);
  
  }, function(error) {
      console.log('Problem with request: ' + error);
  })
  .done();
  </code></pre>
  
  <p>To iterate, I tried to fit that into a for loop:</p>
  
  <pre><code>function iterateRequests() {
      var deferred = Q.defer();
      var reqResults = [];
      for (var iteration = 0; iteration &lt; requests; iteration++) {
          runRequest()
          .then(function(requestDetails) {
              console.log('STATUS: ' + requestDetails.reqStatus);
              reqResults.push(requestDetails);
          }, function(error) {
              console.log('Problem with request: ' + error);
          });
      }
      deferred.resolve(reqResults);
      return deferred.promise;
  }
  </code></pre>
  
  <p>Then I call it like this:</p>
  
  <pre><code>iterateRequests()
  .then(function(results) {
      console.log(results);
      console.log("in the success callback after iterateRequests");
  }, function() {
      console.log("in the failure callback after iterateRequests");
  })
  .done();
  </code></pre>
  
  <p>I end up getting into the success callback (i.e., it logs "in the success callback after iterateRequests"). However, the console.log(results) prints before I get the logs from runRequest().then() callback and it's an empty array.</p>
  
  <p>Any ideas or some guidance on chaining/iterating over promise-return functions?</p>
  
  <p>Thanks!</p>
  
  <p><strong>Update</strong>
  Follow up question in response to @abject_error's answer:</p>
  
  <p>Checked out Q.all. Definitely looks like what I need. And it's much simpler that what I was working with. I made a simple test case to help me figure out how it works:</p>
  
  <pre><code>var Q = require("q");
  
  function returner(number) {
      var deferred = Q.defer();
  
      deferred.resolve(number);
      return deferred.promise;
  }
  
  function parent() {
      return Q.all([
          returner(1),
          returner(2),
          returner(4)
      ]);
  }
  
  
  parent()
  .then(function(promises) {
      // works - promises gives me [1, 2, 4]
      console.log(promises);
  });
  </code></pre>
  
  <p>So I see how I can use it if I know beforehand the number of times I need to call it (and which functions I'm going to call). Any tips on how to get a dynamic number of calls to returner (in this example) or runRequest (in my original example) in the array?</p>