Skip to content

Commit

Permalink
Improve eachBatch method (#1179)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
SebC99 authored Jun 12, 2020
1 parent 0e6af5a commit 8a69f95
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/ParseQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
});
}

Expand Down

0 comments on commit 8a69f95

Please sign in to comment.