title: Javascript Promises confusion
body: <p>I'm trying to figure out Promises with Parse. </p>
  
  <p>What I want to do is get a lot of jobs, and then perform an update for each job. </p>
  
  <pre><code>var queryJobs = new Parse.Query("Jobs");
  queryJobs.find().then(function (results) {
  
      // Loop thorugh all jobs
      for (var i = 0; i &lt; results.length; i++) {
          var job = results[i];
  
          // Here. I want to run an update on all object and then continue. 
      }
  
      return ????;
  
  }).then(function () {
      status.success("Finish");
  }, function () {
      status.error("Error");
  });
  </code></pre>
  
  <p>I tried this without luck. The push block is never executed. </p>
  
  <pre><code>var queryJobs = new Parse.Query("Jobs");
  queryJobs.find().then(function (results) {
  
      var promises = [];
  
      // Loop thorugh all jobs
      for (var i = 0; i &lt; results.length; i++) {
          var job = results[i];
  
  
          promises.push((function () {
  
               // This is never executed. 
  
              var promise = new Parse.Promise();
  
              var query = new Parse.Query("Jobs");
              query.first({
                  success: function (object) {
                      // ... Do something here.... 
                      promise.resolve();
                  },
                  error: function () {
                      promise.resolve();
                  }
              });
  
              promise.resolve();
              return promise;
          }));
      }
  
      return Parse.Promise.when(promises);
  
  }).then(function () {
      status.success("Finish");
  }, function () {
      status.error("Error");
  });
  </code></pre>
  
  <p>Thanks in advance</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I've changed the code, and I get into the callback, however, the query is not executed. </p>
  
  <pre><code>        ...
  
          promises.push((function () {
  
              // GET HERE
              var promise = new Parse.Promise();
  
              var query = new Parse.Query("Jobs");
              query.first({
                  success: function (object) {
                      console.log("CALLBACK");
                      promise.resolve();
                  },
                  error: function () {
                      console.log("CALLBACK");
                      promise.resolve();
                  }
              });
          }()));
  
      return Parse.Promise.when(promises);
  </code></pre>