Skip to content

Commit f06d1c7

Browse files
committed
fix(cursor): wait until all eachAsync() functions finish before resolving the promise
Fix #8352
1 parent d4a7ef3 commit f06d1c7

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

lib/helpers/cursor/eachAsync.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ module.exports = function eachAsync(next, fn, options, callback) {
3535
}
3636
};
3737

38-
const iterate = function(callback) {
38+
const iterate = function(finalCallback) {
3939
let drained = false;
40+
let handleResultsInProgress = 0;
4041

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

5152
next(function(err, doc) {
52-
if (drained || error) {
53+
if (drained || error != null) {
5354
return done();
5455
}
5556
if (err != null) {
5657
error = err;
57-
callback(err);
58+
finalCallback(err);
5859
return done();
5960
}
6061
if (doc == null) {
6162
drained = true;
62-
callback(null);
63+
if (handleResultsInProgress <= 0) {
64+
finalCallback(null);
65+
}
6366
return done();
6467
}
6568

6669
done();
6770

71+
++handleResultsInProgress;
6872
handleNextResult(doc, function(err) {
73+
--handleResultsInProgress;
6974
if (err != null) {
7075
error = err;
71-
return callback(err);
76+
return finalCallback(err);
77+
}
78+
if (drained && handleResultsInProgress <= 0) {
79+
return finalCallback(null);
7280
}
7381

7482
setTimeout(() => enqueue(fetch), 0);

0 commit comments

Comments
 (0)