From 8a69f9504f398187a856797c4b037b8eb6804dd8 Mon Sep 17 00:00:00 2001 From: SebC Date: Fri, 12 Jun 2020 14:06:00 +0200 Subject: [PATCH] Improve eachBatch method (#1179) * Change the promises order of the eachBatch method to allow querying while executing the callback on the previous result as the same time. * Change the "finished" test to take into account the find mock of the tests --- src/ParseQuery.js | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/ParseQuery.js b/src/ParseQuery.js index 8e89e1ddd..10666034f 100644 --- a/src/ParseQuery.js +++ b/src/ParseQuery.js @@ -923,18 +923,23 @@ class ParseQuery { } let finished = false; + let previousResults = []; return continueWhile(() => { return !finished; - }, () => { - return query.find(findOptions).then((results) => { - return Promise.resolve(callback(results)).then(() => { - if (results.length >= query._limit) { - query.greaterThan('objectId', results[results.length - 1].id); - } else { - finished = true; - } - }); - }); + }, async () => { + const [results] = await Promise.all([ + query.find(findOptions), + Promise.resolve(previousResults.length > 0 && callback(previousResults)) + ]); + if (results.length >= query._limit) { + query.greaterThan('objectId', results[results.length - 1].id); + previousResults = results; + } else if (results.length > 0) { + await Promise.resolve(callback(results)); + finished = true; + } else { + finished = true; + } }); }