Skip to content

Instantly share code, notes, and snippets.

@drewlesueur
Created May 29, 2011 00:53
Show Gist options
  • Save drewlesueur/997368 to your computer and use it in GitHub Desktop.
Save drewlesueur/997368 to your computer and use it in GitHub Desktop.
async.js forEach vs forEachSeries
exports['forEachSeries'] = function(test){
var args = [];
async.forEachSeries([1,3,2], function(x, callback){
setTimeout(function(){
args.push(x);
callback();
}, x*25);
}, function(err){
test.same(args, [1,3,2]);
test.done();
});
};
exports['forEachSeries empty array'] = function(test){
test.expect(1);
async.forEachSeries([], function(x, callback){
test.ok(false, 'iterator should not be called');
callback();
}, function(err){
test.ok(true, 'should call callback');
});
setTimeout(test.done, 25);
};
exports['forEachSeries error'] = function(test){
test.expect(2);
var call_order = [];
async.forEachSeries([1,2,3], function(x, callback){
call_order.push(x);
callback('error');
}, function(err){
test.same(call_order, [1]);
test.equals(err, 'error');
});
setTimeout(test.done, 50);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment