title: How to query in loop to fetch all data from class
body: <p>I have a class with (currently) 1567 objects. They are urls,title and publication dates of article which I parse from RSS feeds of websites. The cloud job is periodic, so the objects keep on increasing. Although I check for uniqueness in beforesave, from time to time some duplicate items creep up, approximately 10% of objects are duplicate. </p>
  
  <p>I have been trying to make a job to delete these duplicates and want to make a query logic which can get ALL the objects at once. The maximum limit of query is 1000. I referred to <a href="https://parse.com/questions/fetch-all-data-in-a-table-using-pfquery" rel="nofollow">this question on Parse Help</a>, and tried to convert it into JavaScript cloud code.</p>
  
  <pre><code>Parse.Cloud.job("DeleteDuplicate", function(request, status) {
  
  var query = new Parse.Query(NewsArticle);
  var allObjectArray= [];
  var limit = 1000;
  var skip = 0;
  var repeat = true;
  query.limit(limit);
  query.skip(skip);
  do{
      query.find({
          success: function(results) {
              allObjectArray.push(results.concat());
              if(results.length === limit){
                  skip = skip+limit;
                  query.skip(skip);
                  repeat = true;
                  console.log("true");
              }else{
                  repeat = false;
                  console.log("false");
              }
              console.log("Successfully retrieved " + results.length);
          },
          error: function(error) {
              alert("Error: " + error.code + " " + error.message);
              status.error("Error: " + error.code + " " + error.message);
          }
      });
  }while(repeat);
      status.success("final length "+allObjectArray.length);
  });
  </code></pre>
  
  <p>The code fails with the Job Status of "<strong>Could not connect to Cloud Code</strong>". I think it goes to infinite loop and times out after 2-3 minutes. If anyone can help, that would be great.</p>
  
  <p>EDIT: Can using <code>Promise</code> help?</p>
  
  <hr>
  
  <p>EDIT 2: Been trying Promise now - </p>
  
  <pre><code>Parse.Cloud.job("jobFindAll", function(request, status) {
  
  var query = new Parse.Query(NewsArticle);
  var allObjectArray= [];
  var limit = 1000;
  var skip = 0;
  var repeat = false;
  query.limit(limit);
  query.skip(skip);
  var promiseList = [];
  
  console.log("in job");
  query.find().then(function(results) {
      console.log("results.length "+results.length);
      allObjectArray = results.slice();
      console.log("allObjectArray.length "+allObjectArray.length);
      if(results.length === limit){
          console.log("smaller");
          skip = skip+limit;
          do{
              console.log("first repeat = "+repeat);
              promiseList.push(functionFindAll(allObjectArray, limit, skip));
              console.log("promiseList - "+promiseList);
              repeat = promiseList[promiseList.length-1];
              console.log("looping repeat = "+repeat);
          }while(repeat);
          return Parse.Promise.when(promiseList);
      }else{
          console.log("longer");
      }
  }).then(function(){
          console.log("in then");
          status.success("final length "+allObjectArray.length);
      }, function(error) {
          status.error("Error: " + error.code + " " + error.message);
      }
  );
  });
  
  function functionFindAll(allObjectArray, limit, skip){
  var returnPromiseList = [];
  var query_new = new Parse.Query(NewsArticle);
  query_new.limit(limit);
  query_new.skip(skip);
  query_new.find().then(function(results) {
      console.log("function results.length "+results.length);
      if(results.length === limit){
          skip = skip+limit;
          query.skip(skip);
          allObjectArray.push(results.concat());
          console.log("true in function");
          return Parse.Promise.as(true);
      }else{
          allObjectArray.push(results.concat());
          return Parse.Promise.as(false);
          console.log("false in function");
      }
  },
  function(error) {
      console.log("Error: " + error.code + " " + error.message);
      return Parse.Promise.as("ERROR!");
  }
  );
  console.log("below "+allObjectArray.length);
  }
  </code></pre>
  
  <p>Now the code doesnt enter the query_new.find(). The log messages in the function doesnt appear,  directly the message "below...." appears.</p>