Skip to content

Commit

Permalink
Merge pull request caolan#100 from mmalecki/foreach-no-callback
Browse files Browse the repository at this point in the history
Allow `forEach*` functions to be called with no callback
  • Loading branch information
Caolan McMahon committed Feb 27, 2012
2 parents 7fa56c4 + c82e885 commit 0e90924
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/async.js
Expand Up @@ -77,6 +77,7 @@
}

async.forEach = function (arr, iterator, callback) {
callback = callback || function () {};
if (!arr.length) {
return callback();
}
Expand All @@ -98,6 +99,7 @@
};

async.forEachSeries = function (arr, iterator, callback) {
callback = callback || function () {};
if (!arr.length) {
return callback();
}
Expand All @@ -123,6 +125,7 @@
};

async.forEachLimit = function (arr, limit, iterator, callback) {
callback = callback || function () {};
if (!arr.length || limit <= 0) {
return callback();
}
Expand Down
18 changes: 18 additions & 0 deletions test/test-async.js
Expand Up @@ -37,6 +37,12 @@ function detectIterator(call_order, x, callback) {
}, x*25);
}

function forEachNoCallbackIterator(test, x, callback) {
test.equal(x, 1);
callback();
test.done();
}

function getFunctionsObject(call_order) {
return {
one: function(callback){
Expand Down Expand Up @@ -514,6 +520,10 @@ exports['forEach error'] = function(test){
setTimeout(test.done, 50);
};

exports['forEach no callback'] = function(test){
async.forEach([1], forEachNoCallbackIterator.bind(this, test));
};

exports['forEachSeries'] = function(test){
var args = [];
async.forEachSeries([1,3,2], forEachIterator.bind(this, args), function(err){
Expand Down Expand Up @@ -546,6 +556,10 @@ exports['forEachSeries error'] = function(test){
setTimeout(test.done, 50);
};

exports['forEachSeries no callback'] = function(test){
async.forEachSeries([1], forEachNoCallbackIterator.bind(this, test));
};

exports['forEachLimit'] = function(test){
var args = [];
var arr = [0,1,2,3,4,5,6,7,8,9];
Expand Down Expand Up @@ -617,6 +631,10 @@ exports['forEachLimit error'] = function(test){
setTimeout(test.done, 25);
};

exports['forEachLimit no callback'] = function(test){
async.forEachLimit([1], 1, forEachNoCallbackIterator.bind(this, test));
};

exports['map'] = function(test){
var call_order = [];
async.map([1,3,2], mapIterator.bind(this, call_order), function(err, results){
Expand Down

0 comments on commit 0e90924

Please sign in to comment.