Skip to content

Commit 8a69f95

Browse files
authored
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
1 parent 0e6af5a commit 8a69f95

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

src/ParseQuery.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -923,18 +923,23 @@ class ParseQuery {
923923
}
924924

925925
let finished = false;
926+
let previousResults = [];
926927
return continueWhile(() => {
927928
return !finished;
928-
}, () => {
929-
return query.find(findOptions).then((results) => {
930-
return Promise.resolve(callback(results)).then(() => {
931-
if (results.length >= query._limit) {
932-
query.greaterThan('objectId', results[results.length - 1].id);
933-
} else {
934-
finished = true;
935-
}
936-
});
937-
});
929+
}, async () => {
930+
const [results] = await Promise.all([
931+
query.find(findOptions),
932+
Promise.resolve(previousResults.length > 0 && callback(previousResults))
933+
]);
934+
if (results.length >= query._limit) {
935+
query.greaterThan('objectId', results[results.length - 1].id);
936+
previousResults = results;
937+
} else if (results.length > 0) {
938+
await Promise.resolve(callback(results));
939+
finished = true;
940+
} else {
941+
finished = true;
942+
}
938943
});
939944
}
940945

0 commit comments

Comments
 (0)