From a0ffde83e42ae407514faaa8e754912d91df713f Mon Sep 17 00:00:00 2001 From: Hubert Argasinski Date: Wed, 21 Jun 2017 23:07:18 -0400 Subject: PR fixes --- lib/concatLimit.js | 3 ++- mocha_test/concat.js | 26 ++++++++++++++++++-------- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/lib/concatLimit.js b/lib/concatLimit.js index 8b8f5d8..4df03ea 100644 --- a/lib/concatLimit.js +++ b/lib/concatLimit.js @@ -3,6 +3,8 @@ import wrapAsync from './internal/wrapAsync'; import slice from './internal/slice'; import mapLimit from './mapLimit'; +var _concat = Array.prototype.concat; + /** * The same as [`concat`]{@link module:Collections.concat} but runs a maximum of `limit` async operations at a time. * @@ -31,7 +33,6 @@ export default function(coll, limit, iteratee, callback) { }); }, function(err, mapResults) { var result = []; - var _concat = Array.prototype.concat; for (var i = 0; i < mapResults.length; i++) { if (mapResults[i]) { result = _concat.apply(result, mapResults[i]); diff --git a/mocha_test/concat.js b/mocha_test/concat.js index f84a84b..f6b73b3 100644 --- a/mocha_test/concat.js +++ b/mocha_test/concat.js @@ -307,9 +307,9 @@ describe('concat', function() { it('does not continue replenishing after error', function(done) { var started = 0; var arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; - var delay = 10; var limit = 3; - var maxTime = 10 * arr.length; + var step = 0; + var maxSteps = arr.length; async.concatLimit(arr, limit, function(val, next) { started++; @@ -317,18 +317,28 @@ describe('concat', function() { return next(new Error('fail')); } - setTimeout(function() { + async.setImmediate(function() { next(); - }, delay); + }); }, function(err, result) { expect(err).to.not.eql(null); expect(result).to.be.an('array').that.is.empty; }); - setTimeout(function() { - expect(started).to.equal(3); - done(); - }, maxTime); + // wait `maxSteps` event loop cycles before calling done to ensure + // the iteratee is not called on more items in arr. + function waitCycle() { + step++; + if (step >= maxSteps) { + expect(started).to.equal(3); + done(); + return; + } else { + async.setImmediate(waitCycle); + } + } + + async.setImmediate(waitCycle); }); }); -- cgit v1.2.1