Skip to content

Commit

Permalink
adding forEachOf to iterate objects asynchronously
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominic Barnes authored and Dominic Barnes committed Feb 7, 2013
1 parent 914fa10 commit e0a51c3
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
29 changes: 29 additions & 0 deletions lib/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@
return memo;
};

var _forEachOf = function (object, iterator) {
for (key in object) {
if (object.hasOwnProperty(key)) {
iterator(object[key], key);
}
}
};

var _keys = function (obj) {
if (Object.keys) {
return Object.keys(obj);
Expand Down Expand Up @@ -111,6 +119,27 @@
});
};

async.forEachOf = function (object, iterator, callback) {
callback = callback || function () {};
var completed = 0, size = _keys(object).length, key;
if (!size) {
return callback();
}
_forEachOf(object, function (value, key) {
iterator(object[key], key, function (err) {
if (err) {
callback(err);
callback = function () {};
} else {
completed += 1;
if (completed === size) {
callback(null);
}
}
});
});
};

async.forEachSeries = function (arr, iterator, callback) {
callback = callback || function () {};
if (!arr.length) {
Expand Down
47 changes: 47 additions & 0 deletions test/test-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ function forEachIterator(args, x, callback) {
}, x*25);
}

function forEachOfIterator(args, x, key, callback) {
setTimeout(function(){
args.push(key, x);
callback();
}, x*25);
}

function mapIterator(call_order, x, callback) {
setTimeout(function(){
call_order.push(x);
Expand All @@ -43,6 +50,13 @@ function forEachNoCallbackIterator(test, x, callback) {
test.done();
}

function forEachOfNoCallbackIterator(test, x, key, callback) {
test.equal(x, 1);
test.equal(key, "a");
callback();
test.done();
}

function getFunctionsObject(call_order) {
return {
one: function(callback){
Expand Down Expand Up @@ -652,6 +666,39 @@ exports['forEach no callback'] = function(test){
async.forEach([1], forEachNoCallbackIterator.bind(this, test));
};

exports['forEachOf'] = function(test){
var args = [];
async.forEachOf({ a: 1, b: 2 }, forEachOfIterator.bind(this, args), function(err){
test.same(args, ["a", 1, "b", 2]);
test.done();
});
};

exports['forEachOf empty object'] = function(test){
test.expect(1);
async.forEachOf({}, function(value, key, callback){
test.ok(false, 'iterator should not be called');
callback();
}, function(err) {
test.ok(true, 'should call callback');
});
setTimeout(test.done, 25);
};

exports['forEachOf error'] = function(test){
test.expect(1);
async.forEachOf({ a: 1, b: 2 }, function(value, key, callback) {
callback('error');
}, function(err){
test.equals(err, 'error');
});
setTimeout(test.done, 50);
};

exports['forEachOf no callback'] = function(test){
async.forEachOf({ a: 1 }, forEachOfNoCallbackIterator.bind(this, test));
};

exports['forEachSeries'] = function(test){
var args = [];
async.forEachSeries([1,3,2], forEachIterator.bind(this, args), function(err){
Expand Down

0 comments on commit e0a51c3

Please sign in to comment.