Skip to content

Commit

Permalink
fix(cursor): wait for all promises to resolve if parallel is greate…
Browse files Browse the repository at this point in the history
…r than number of documents

Fix #8422
  • Loading branch information
vkarpov15 committed Dec 20, 2019
1 parent ce6d4ba commit 3133e50
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
5 changes: 4 additions & 1 deletion lib/helpers/cursor/eachAsync.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,12 @@ module.exports = function eachAsync(next, fn, options, callback) {
return done();
}

++handleResultsInProgress;

// Kick off the subsequent `next()` before handling the result, but
// make sure we know that we still have a result to handle re: #8422
done();

++handleResultsInProgress;
handleNextResult(doc, function(err) {
--handleResultsInProgress;
if (err != null) {
Expand Down
8 changes: 4 additions & 4 deletions test/query.cursor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -494,15 +494,15 @@ describe('QueryCursor', function() {
{ name: 'Ip Man' },
{ name: 'Enter the Dragon' }
]);

let numDone = 0;

const test = co.wrap(function*() {
yield new Promise((resolve) => setTimeout(resolve, 100));
++numDone;
});
yield Movie.find().cursor().eachAsync(test, { parallel: 4 })

yield Movie.find().cursor().eachAsync(test, { parallel: 4 });
assert.equal(numDone, 3);
});
});
Expand Down

0 comments on commit 3133e50

Please sign in to comment.