Skip to content

Commit

Permalink
fix(cursor): wait until all eachAsync() functions finish before res…
Browse files Browse the repository at this point in the history
…olving the promise

Fix #8352
  • Loading branch information
vkarpov15 committed Dec 4, 2019
1 parent d4a7ef3 commit f06d1c7
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions lib/helpers/cursor/eachAsync.js
Expand Up @@ -35,8 +35,9 @@ module.exports = function eachAsync(next, fn, options, callback) {
}
};

const iterate = function(callback) {
const iterate = function(finalCallback) {
let drained = false;
let handleResultsInProgress = 0;

let error = null;
for (let i = 0; i < parallel; ++i) {
Expand All @@ -49,26 +50,33 @@ module.exports = function eachAsync(next, fn, options, callback) {
}

next(function(err, doc) {
if (drained || error) {
if (drained || error != null) {
return done();
}
if (err != null) {
error = err;
callback(err);
finalCallback(err);
return done();
}
if (doc == null) {
drained = true;
callback(null);
if (handleResultsInProgress <= 0) {
finalCallback(null);
}
return done();
}

done();

++handleResultsInProgress;
handleNextResult(doc, function(err) {
--handleResultsInProgress;
if (err != null) {
error = err;
return callback(err);
return finalCallback(err);
}
if (drained && handleResultsInProgress <= 0) {
return finalCallback(null);
}

setTimeout(() => enqueue(fetch), 0);
Expand Down

0 comments on commit f06d1c7

Please sign in to comment.