title: Parse.com Cloud job promise with nested query
body: <p>I have a game on Parse.com where users make submissions and other players vote on them.</p>
  
  <p>I'm trying to write a Cloud background job to update all my user's accounts with vote totals which are the sum of the vote totals from each of their submissions.</p>
  
  <p>I have tried this about 5 different ways and cannot get the nested queries to run (let alone update and save).</p>
  
  <p>Here are the docs and a helpful blog post...</p>
  
  <p><a href="https://parse.com/docs/js_guide#promises-creation" rel="nofollow">https://parse.com/docs/js_guide#promises-creation</a></p>
  
  <p><a href="http://blog.parse.com/2013/01/29/whats-so-great-about-javascript-promises/" rel="nofollow">http://blog.parse.com/2013/01/29/whats-so-great-about-javascript-promises/</a></p>
  
  <p>Here is my latest, any help is greatly appreciated...</p>
  
  <p><strong>Edit:</strong>  Thanks @axymous for making me see clearly, I needed to explicitly create and immediately return the promise then resolve it in the callback.</p>
  
  <pre><code>Parse.Cloud.job("calculateUserTotals", function(request, status) {
      Parse.Cloud.useMasterKey();
      var query = new Parse.Query(Parse.User);
      query.find().then(function(users) {
          var promises = [];
          _.each(users, function(user) {
              promises.push((function(user){
                  var promise = new Parse.Promise();
                  console.log(user.toJSON().objectId);  // user objectId is correct here
                  var submissionQuery = new Parse.Query('submission');
                  submissionQuery.equalTo('user', user.toJSON().objectId);
                  submissionQuery.find({  // this doesnt seem to ever run, i have tried .each as well
                      success: function(submissions) {
                          var votes = 0;
                          for (var i=0; i&lt;submissions.length; i++) {
                              var submission = submissions[i];
                              votes = votes + submission.get('votes').length;
                          }
                          user.set('submission_count', submissions.length);
                          user.set('vote_count', votes);
                          user.save({
                              success: function(user) {
                                  promise.resolve();
                              }
                          });
                      },
                      error: function(error) {
                          status.error(error);
                      }
                  });
                  return promise;
              })(user));
          });
          return Parse.Promise.when(promises);
      })
      .then(function() {
          status.success('leaderboard updated.');
      });
  });
  </code></pre>