title: Asynchronous function call inside for loop
body: <p>I am having a problem trying to make my for loop behave synchronously when I am making an asynchronous call in each iteration of the loop.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>pipeline: function(userContext, options, done){
  
      var orderData = [];
  
      options.data.forEach(function(store, index){
          store.orders.forEach(function(order){
              shopify.getPipeline(userContext, {'order':order,'storeId':index}, function(result){
                  var snapshotId = "30775bf1bb854c5d84c9c2af37bc8fb0";
                  var resourceToQuery = config.structrUrls.getUrl("ConfigurationSnapshot") + '/' + snapshotId ;
                  var requestOptions = {
                      method: "GET",
                      timeout: 8000
                  };
                  requestify.request(resourceToQuery, requestOptions)
                      .then(function(snapshotResult){
                          result.snapshots = snapshotResult.getBody().result;
                          result.snapshots.configurationPayload = JSON.parse(snapshotResult.getBody().result.configurationPayload);
                          orderData.push(result);
                      })
                      .catch(function(err){
                          console.log (err);
                          done(err);
                      });
              });
          });
      });
  
      done(null, orderData);
  
  }
  </code></pre>
  
  <p>I understand the problem here, but do not know how to remedy it. Let me explain the function:</p>
  
  <p>options.data contains an array of stores, and each store contains an array of orders. For each order, I am calling shopify.getPipeline() for pipeline data (this is a synchronous operation), and in the callback I make a requestify request (a node module used for making http requests) for snapshot data, which I want to append to the result before pushing it onto my "orderData" array. When this all completes, I am calling "done", a callback function, with my orderData. As you can see, since requestify calls are asynchronous, done is called before any data is added to the orderData array.</p>
  
  <p>I think I need to use some kind of promise in order to guarantee the result before calling done, but I have been unsuccessful in implementing a promise into this function. In the documentation for <a href="https://github.com/kriskowal/q/wiki/API-Reference" rel="nofollow">q</a>, it seems like the function I would want to use is promise.all(), which 'Returns a promise that is fulfilled with an array containing the fulfillment value of each promise, or is rejected with the same rejection reason as the first promise to be rejected'. I'm failing to see how to translate my forEach loop into an array of promises. I was also looking at the <a href="https://github.com/caolan/async#paralleltasks-callback" rel="nofollow">async parallel function</a> and ran into the same problem regarding the array of functions.</p>
  
  <p>Any help is greatly appreciated. Thanks!</p>