title: Javascript - run for-loop with promises
body: <p>I have a javascript script that I am running on parse.com that send a push notification to a user. I have the script almost done. Right now it will send a push with the query set to where I have it. So here are the restraints I have on it right now.</p>
  
  <pre><code>testItemsQuery.equalTo('school', 'Union College (NY)'); 
  testItemsQuery.equalTo('meal','LUNCH');
  </code></pre>
  
  <p>But now I have 2 variables </p>
  
  <pre><code>var schoolArray = ['Union College (NY)', 'University of Albany'];
  var mealArray = ['LUNCH','DINNER'];
  </code></pre>
  
  <p>And I want it too loop through this script a total of 4 times. I first want it to loop through for both schools and then loop through each meal. So the most basic version of code would look like this.</p>
  
  <pre><code>var schoolArray = ['Union College (NY)', 'University of Albany'];
  var mealArray = ['LUNCH','DINNER'];
  
  for (var i = 0; i &lt; schoolArray.length; i++) {
      for (var j = 0; j &lt; mealArray.length; j++) {
           //insert all my other code here
      }
  }
  </code></pre>
  
  <p>Here is my script which makes async requests to parse.com so that is why the for loops aren't working because I think I need to use some promises. Heres the code I want inside the for loop:</p>
  
  <pre><code>    function SendPush(installationId, msg) {
  
      var query = new Parse.Query(Parse.Installation);
      query.equalTo('objectId', installationId);
      Parse.Push.send({
        where: query,
        data: {alert: msg}
      });
  } 
  var schoolArray = ['Union College (NY)', 'University of Albany'];
  var mealArray = ['LUNCH','DINNER'];
  
  var TestItem = Parse.Object.extend("TestItem");
  var UserFavorites = Parse.Object.extend("UserFavourite");   
  var testItemsQuery = new Parse.Query(TestItem);
  
  
  testItemsQuery.equalTo('school', 'Union College (NY)'); //I want to loop through both schools for both meals
  testItemsQuery.equalTo('meal','LUNCH');
  
  
  var groupedAlerts = {};
  testItemsQuery.each(function(testItem) {
      var item = testItem.get('item');
      var school = testItem.get('school');
      var userFavoritesQuery = new Parse.Query(UserFavorites); 
      userFavoritesQuery.include('user');
      userFavoritesQuery.equalTo('item', item);
      userFavoritesQuery.equalTo('school', school);
  
      return userFavoritesQuery.each(function(userFavorite) {
  
          //console.log(userFavorite);
          var user = userFavorite.get('user');
          if(user){
              var instId = user.get('installation').id;
              //console.log(instId);
              if(!groupedAlerts[instId]) {
                  groupedAlerts[instId] = [];
              }
              var  meal = testItem.get('meal').toLowerCase();
              var m = userFavorite.get('item') + " is being served for " + meal + " at " + testItem.get('schoolMenu');
              groupedAlerts[instId].push(m);
          }
      });
  }).then(function () {
      var alerts = [];
      for(var key in groupedAlerts) {
          alerts.push({
              installationId: key,
              alerts: groupedAlerts[key],
          });
          // Send push notifications
          SendPush(key, groupedAlerts[key].join());
      }
  });
  </code></pre>
  
  <p>Thanks for the help in advance!!!!</p>