Permalink
Browse files

Merge pull request #100 from mmalecki/foreach-no-callback

Allow `forEach*` functions to be called with no callback
  • Loading branch information...
2 parents 7fa56c4 + c82e885 commit 0e9092493f245388eb4fcfc07b30570907def74b @caolan committed Feb 27, 2012
Showing with 21 additions and 0 deletions.
  1. +3 −0 lib/async.js
  2. +18 −0 test/test-async.js
View
@@ -77,6 +77,7 @@
}
async.forEach = function (arr, iterator, callback) {
+ callback = callback || function () {};
if (!arr.length) {
return callback();
}
@@ -98,6 +99,7 @@
};
async.forEachSeries = function (arr, iterator, callback) {
+ callback = callback || function () {};
if (!arr.length) {
return callback();
}
@@ -123,6 +125,7 @@
};
async.forEachLimit = function (arr, limit, iterator, callback) {
+ callback = callback || function () {};
if (!arr.length || limit <= 0) {
return callback();
}
View
@@ -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){
@@ -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){
@@ -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];
@@ -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){

0 comments on commit 0e90924

Please sign in to comment.