Skip to content

Commit

Permalink
add timesLimit. Closes #743
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Early committed Jun 2, 2015
1 parent 62bba6e commit f1263b3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 24 deletions.
26 changes: 13 additions & 13 deletions lib/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@
return result;
}

function _range(count) {
return _map(Array(count), function (v, i) { return i; });
}

function _reduce(arr, iterator, memo) {
_arrayEach(arr, function (x, i, a) {
memo = iterator(memo, x, i, a);
Expand Down Expand Up @@ -1056,20 +1060,16 @@
};
};

async.times = function (count, iterator, callback) {
var counter = [];
for (var i = 0; i < count; i++) {
counter.push(i);
}
return async.map(counter, iterator, callback);
};
function _times(mapper) {
return function (count, iterator, callback) {
mapper(_range(count), iterator, callback);
};
}

async.timesSeries = function (count, iterator, callback) {
var counter = [];
for (var i = 0; i < count; i++) {
counter.push(i);
}
return async.mapSeries(counter, iterator, callback);
async.times = _times(async.map);
async.timesSeries = _times(async.mapSeries);
async.timesLimit = function (count, limit, iterator, callback) {
return async.mapLimit(_range(count), limit, iterator, callback);
};

async.seq = function (/* functions... */) {
Expand Down
42 changes: 31 additions & 11 deletions test/test-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -2347,18 +2347,19 @@ var console_fn_tests = function(name){
};


exports['times'] = {

exports['times'] = function(test) {
'times': function(test) {
async.times(5, function(n, next) {
next(null, n);
}, function(err, results) {
test.ok(err === null, err + " passed instead of 'null'");
test.same(results, [0,1,2,3,4]);
test.done();
});
};
},

exports['times'] = function(test){
'times 3': function(test){
var args = [];
async.times(3, function(n, callback){
setTimeout(function(){
Expand All @@ -2370,9 +2371,9 @@ exports['times'] = function(test){
test.same(args, [0,1,2]);
test.done();
});
};
},

exports['times 0'] = function(test){
'times 0': function(test){
test.expect(1);
async.times(0, function(n, callback){
test.ok(false, 'iterator should not be called');
Expand All @@ -2382,19 +2383,19 @@ exports['times 0'] = function(test){
test.ok(true, 'should call callback');
});
setTimeout(test.done, 25);
};
},

exports['times error'] = function(test){
'times error': function(test){
test.expect(1);
async.times(3, function(n, callback){
callback('error');
}, function(err){
test.equals(err, 'error');
});
setTimeout(test.done, 50);
};
},

exports['timesSeries'] = function(test){
'timesSeries': function(test){
var call_order = [];
async.timesSeries(5, function(n, callback){
setTimeout(function(){
Expand All @@ -2406,16 +2407,35 @@ exports['timesSeries'] = function(test){
test.same(results, [0,1,2,3,4]);
test.done();
});
};
},

exports['timesSeries error'] = function(test){
'timesSeries error': function(test){
test.expect(1);
async.timesSeries(5, function(n, callback){
callback('error');
}, function(err){
test.equals(err, 'error');
});
setTimeout(test.done, 50);
},

'timesLimit': function(test){
var limit = 2;
var running = 0;
async.timesLimit(5, limit, function (i, next) {
running++;
test.ok(running <= limit && running > 0, running);
setTimeout(function () {
running--;
next(null, i * 2);
}, (3 - i) * 10);
}, function(err, results){
test.ok(err === null, err + " passed instead of 'null'");
test.same(results, [0, 2, 4, 6, 8]);
test.done();
});
}

};

console_fn_tests('log');
Expand Down

0 comments on commit f1263b3

Please sign in to comment.