Q:

Parse Cloud Code Promise not returning correctly

I thought I had this Promises thing figured out, but this function to get all results from a Query regardless of the 1000 item limit isn't working correctly. Could anyone explain to me where I'm going wrong here? Thanks!
This Cloud Job is in my main.js file:
//in main.js
Parse.Cloud.job("testQuery", function(request, status) {
  var theClass = Parse.Object.extend("SomeClass");
  var theQuery = new Parse.Query(theClass);
  console.log("Here we go...");
  queryHelper.getAllResultsForQuery(theQuery, console).then( function(result) {
    //This code is called before getAllResultsForQuery finishes
    console.log("Finished the search!");
    status.success("yay");
  }, function(error) {
    console.log("Fail " + error);
    status.error("failed!");
  });
});

And this function is in queryHelper.js:
//in queryHelper.js
exports.getAllResultsForQuery = function(query, console) {
  var resultArray = [];
  var limit = 1000;

  var sendQuery = function(skip) {
    if (skip) {
      query.greaterThan("createdAt", skip);
    }
    query.limit(limit);
    query.ascending("createdAt");
    query.find().then(function (newResults) {
      receivedResults(newResults);
    }, function (error) {
      return Parse.Promise.error(new Error("query failed after " + resultArray.length + " results, error:" + error));
    });
  };

  var receivedResults = function(received) {
    resultArray = resultArray.concat(received);
    console.log("Got " + received.length + " objects, now at " + resultArray.length);
    if (received.length == limit) {
      sendQuery(received[received.length-1].createdAt);
    } else {
      console.log("returning from getAllResults...");
      return resultArray;
    }
  };

  sendQuery();
};

When I run the code, the queryHelper.getAllResultsForQuery function seems to run just fine, but the Promise seems to fire before that function is finished, so the console output looks like this:
"Here we go…";
"Finished the search!";
"Got 1000 objects, now at 1000";
"Got 1000 objects, now at 2000";
"Got 1000 objects, now at 3000";
"Got 245 objects, now at 3245";
"returning from getAllResults…";

Am I making a rookie mistake somewhere?

A:

getAllResultsForQuery should return a promise directly. I marked added returns.
exports.getAllResultsForQuery = function(query, console) {
    var resultArray = [];
    var limit = 1000;

    var sendQuery = function(skip) {
        if (skip) {
            query.greaterThan("createdAt", skip);
        }
        query.limit(limit);
        query.ascending("createdAt");
        return query.find().then(function (newResults) { //return added here
            return receivedResults(newResults); //return added here
        }, function (error) {
            return Parse.Promise.error(new Error("query failed after " + 
                resultArray.length + " results, error:" + error));
        });
    };

    var receivedResults = function(received) {
        resultArray = resultArray.concat(received);
        console.log("Got " + received.length + " objects, now at " + 
          resultArray.length);
        if (received.length == limit) {
            return sendQuery(received[received.length-1].createdAt).then(function (received) {
                //added code here
                resultArray.concat(results);
                return resultArray;
            }); //return added here
        } else {
            console.log("returning from getAllResults...");
            return resultArray;
        }
     };
     return sendQuery(); //return added here
};

