Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add concatLimit [fixes #1426] #1430

Merged
merged 1 commit into from Jun 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions lib/concatLimit.js
@@ -0,0 +1,22 @@
import doParallelLimit from './internal/doParallelLimit';
import concat from './internal/concat';

/**
* The same as [`concat`]{@link module:Collections.concat} but runs a maximum of `limit` async operations at a time.
*
* @name concatLimit
* @static
* @memberOf module:Collections
* @method
* @see [async.concat]{@link module:Collections.concat}
* @category Collection
* @param {Array|Iterable|Object} coll - A collection to iterate over.
* @param {number} limit - The maximum number of async operations at a time.
* @param {AsyncFunction} iteratee - A function to apply to each item in `coll`,
* which should use an array as its result. Invoked with (item, callback).
* @param {Function} [callback(err)] - A callback which is called after all the
* `iteratee` functions have finished, or an error occurs. Results is an array
* containing the concatenated results of the `iteratee` function. Invoked with
* (err, results).
*/
export default doParallelLimit(concat);
3 changes: 3 additions & 0 deletions lib/index.js
Expand Up @@ -72,6 +72,7 @@ import autoInject from './autoInject';
import cargo from './cargo';
import compose from './compose';
import concat from './concat';
import concatLimit from './concatLimit';
import concatSeries from './concatSeries';
import constant from './constant';
import detect from './detect';
Expand Down Expand Up @@ -150,6 +151,7 @@ export default {
cargo: cargo,
compose: compose,
concat: concat,
concatLimit: concatLimit,
concatSeries: concatSeries,
constant: constant,
detect: detect,
Expand Down Expand Up @@ -246,6 +248,7 @@ export {
cargo as cargo,
compose as compose,
concat as concat,
concatLimit as concatLimit,
concatSeries as concatSeries,
constant as constant,
detect as detect,
Expand Down
37 changes: 37 additions & 0 deletions mocha_test/concat.js
Expand Up @@ -54,4 +54,41 @@ describe('concat', function() {
done();
});
});

it('concatLimit basics', function(done) {
var running = 0;
var concurrency = {
'foo': 2,
'bar': 2,
'baz': 1
};

async.concatLimit(['foo', 'bar', 'baz'], 2, function(val, next) {
running++;
async.setImmediate(function() {
expect(running).to.equal(concurrency[val]);
running--;
next(null, [val, val]);
});
}, function(err, result) {
expect(running).to.equal(0);
expect(err).to.eql(null);
expect(result).to.eql(['foo', 'foo', 'bar', 'bar', 'baz', 'baz']);
done();
});
});

it('concatLimit error', function(done) {
var arr = ['foo', 'bar', 'baz'];
async.concatLimit(arr, 2, function(val, next) {
if (val === 'bar') {
return next(new Error('fail'));
}
next(null, [val, val]);
}, function(err, result) {
expect(err).to.not.eql(null);
expect(result).to.eql(['foo', 'foo']);
done();
});
});
});