title: Javascript Promise node.js?
body: <p>I'm a node.js newbie and I'm trying to understand how I can organize some logic in the non-blocking way node likes it.</p>
  
  <p>I have a set of environments ['stage','prod'], and another set of parameters called brands ['A','B','C'] and a set of devices ['phone','tablet'].</p>
  
  <p>In node's callback-driven world I have this:</p>
  
  <pre><code>brands.forEach( function(brand) {
      devices.forEach( function(device) {
          var tapeS = getTape('stage',brand,device); // bad example...tapeS never set
          var tapeP = getTape('prod' ,brand,device);
      })
  } )
  // more stuff here
  function getTape(env,brand,device) {
     var req = http.request(someOptions,function(resp) {
         // ok, so we handle the response here, but how do I sequence this with all the other
         // responses, also happening asynchronously?
     });
  }
  </code></pre>
  
  <p>I'm trying to build a report with blocks for each environment:</p>
  
  <pre><code>A:
      Stage -- report
      Prod  -- report 
  B:    ...
  </code></pre>
  
  <p>My problem is that since everything here is so async, especially inside getTape, which calls node's http.request.  How can I serialize everything at the end of all this async wonderment so I can create the report in the order I want?</p>
  
  <p>I heard something about javascript Promises.  Would that help, i.e. some way to collect all these Promises then wait for them all to complete, then get the data they collected?</p>