summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHubert Argasinski <argasinski.hubert@gmail.com>2017-06-21 23:07:18 -0400
committerHubert Argasinski <argasinski.hubert@gmail.com>2017-06-21 23:07:18 -0400
commita0ffde83e42ae407514faaa8e754912d91df713f (patch)
tree41cc14d34e5c528a614bfcb59a9021d6eb5922e6
parent39b20e6da268b2b737df7b068d32620b59011474 (diff)
downloadasync-concat-order.tar.gz
PR fixesconcat-order
-rw-r--r--lib/concatLimit.js3
-rw-r--r--mocha_test/concat.js26
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);
});
});