title: javascript - then/ promises can't use variable after for loop
body: <p>I have this code but I am unable to use this userArray outside of the for loop, not sure why it returns nothing, here is the code:</p>
  
  <pre><code>Parse.Cloud.define("test", function(request, response) {
  
  var UserFavourite = Parse.Object.extend("UserFavourite");
  var queryFavourite = new Parse.Query(UserFavourite);
  
  var userArray = [];
  var TestItem = Parse.Object.extend("TestItem");
  var query = new Parse.Query(TestItem);
  query.limit(1000);
  query.equalTo('school', 'Union College (NY)');
  query.find().then(function(results) {
      return results;
  
  
  }).then(function(results) {
      for (var i = 0; i &lt; results.length; i++) { 
          var object = results[i];
          var item = object.get('item');
          var school = object.get('school');
          var meal = object.get('meal');
  
          var UserFavourite = Parse.Object.extend("UserFavourite");
          var queryFavourite = new Parse.Query(UserFavourite);
          queryFavourite.equalTo("item", item);
          queryFavourite.equalTo("school", school);
          queryFavourite.find().then(function(users) {
              for (var i = 0; i &lt; users.length; i++) {
                  var user = users[i];
                  var userID = user.get('user').id;
                  if (userArray.indexOf(userID) === -1) {
                      userArray.push(userID);
                  }
  //                  console.log(userArray);
                  return userArray;
              }
              return userArray;
  
  
          });            
          console.log('sadf '+userArray);
      }
      console.log('sadf '+userArray);
      return userArray;
  
  
  }).then(function() {
      console.log(userArray);
  });
  </code></pre>
  
  <p>I have taken a look at this but I still can seem to be able to get userArray in the last function or in the two console.log's before it</p>
  
  <p><a href="https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call">How do I return the response from an asynchronous call?</a></p>
  
  <p>Thanks for the help in advance.</p>