Skip to content

Commit e60db1b

Browse files
Valeri KarpovValeri Karpov
Valeri Karpov
authored and
Valeri Karpov
committed
refactor(cursor): remove dependency on async.times()
Re: #8073 Re: #5502
1 parent c5b2355 commit e60db1b

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

lib/helpers/cursor/eachAsync.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,25 @@ module.exports = function eachAsync(next, fn, options, callback) {
3838
const iterate = function(callback) {
3939
let drained = false;
4040
const nextQueue = async.queue(function(task, cb) {
41-
if (drained) return cb();
41+
if (drained) {
42+
return cb();
43+
}
4244
next(function(err, doc) {
4345
if (err) return cb(err);
44-
if (!doc) drained = true;
4546
cb(null, doc);
4647
});
4748
}, 1);
4849

4950
const getAndRun = function(cb) {
5051
nextQueue.push({}, function(err, doc) {
5152
if (err) return cb(err);
52-
if (!doc) return cb();
53+
if (drained) {
54+
return;
55+
}
56+
if (doc == null) {
57+
drained = true;
58+
return callback(null);
59+
}
5360
handleNextResult(doc, function(err) {
5461
if (err) return cb(err);
5562
// Make sure to clear the stack re: gh-4697
@@ -60,9 +67,18 @@ module.exports = function eachAsync(next, fn, options, callback) {
6067
});
6168
};
6269

63-
async.times(parallel, function(n, cb) {
64-
getAndRun(cb);
65-
}, callback);
70+
let error = null;
71+
for (let i = 0; i < parallel; ++i) {
72+
getAndRun(err => {
73+
if (error != null) {
74+
return;
75+
}
76+
if (err != null) {
77+
error = err;
78+
return callback(err);
79+
}
80+
});
81+
}
6682
};
6783

6884
return utils.promiseOrCallback(callback, cb => {

test/aggregate.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,10 +1288,10 @@ describe('aggregate: ', function() {
12881288
return MyModel.aggregate([{ $sort: { name: 1 } }]).
12891289
cursor().
12901290
exec().
1291-
eachAsync(checkDoc, { parallel: 2}).then(function() {
1291+
eachAsync(checkDoc, { parallel: 2 }).then(function() {
12921292
assert.ok(Date.now() - startedAt[1] >= 100);
12931293
assert.equal(startedAt.length, 2);
1294-
assert.ok(startedAt[1] - startedAt[0] < 50);
1294+
assert.ok(startedAt[1] - startedAt[0] < 50, `${startedAt[1] - startedAt[0]}`);
12951295
assert.deepEqual(names.sort(), expectedNames);
12961296
done();
12971297
});

0 commit comments

Comments
 (0)