Skip to content
Browse files

added iterator method

  • Loading branch information...
1 parent 8c7751a commit 4930440b1ff75d97b00322fe3f371bad7985e6c1 @caolan committed May 19, 2010
Showing with 83 additions and 21 deletions.
  1. +34 −21 lib/async.js
  2. +49 −0 test/test-async.js
View
55 lib/async.js
@@ -50,18 +50,15 @@ exports.auto = function(tasks, callback){
};
exports.waterfall = function(arr){
- var makeCallback = function(index){
- if(index < arr.length){
- return function(){
- var args = Array.prototype.slice.call(arguments);
- if(index < arr.length-1){
- args = args.concat(makeCallback(index+1));
- }
- process.nextTick(function(){arr[index].apply(null, args);});
- }
- }
+ var wrapIterator = function(iterator){
+ return function(){
+ var args = Array.prototype.slice.call(arguments);
+ var next = iterator.next();
+ if(next) args.push(wrapIterator(next));
+ process.nextTick(function(){iterator.apply(null, args)});
+ };
};
- makeCallback(0)();
+ wrapIterator(exports.iterator(arr))();
};
exports.parallel = function(arr, callback){
@@ -79,17 +76,33 @@ exports.parallel = function(arr, callback){
exports.series = function(arr, callback){
var results = [];
- var makeCallback = function(index){
+ var saveArgs = function(fn){
return function(){
- if(index > 0){
- var args = Array.prototype.slice.call(arguments);
- results.push((args.length > 1) ? args: args[0]);
- }
- if(index < arr.length){
- arr[index](makeCallback(index+1));
- }
- else callback(results);
+ var args = Array.prototype.slice.call(arguments);
+ results.push((args.length > 1) ? args: args[0]);
+ fn.apply(null, args);
+ }
+ };
+ var wrapIterator = function(iterator){
+ return saveArgs(function(){
+ var next = iterator.next();
+ if(next) iterator(wrapIterator(iterator.next()));
+ else iterator(saveArgs(function(){callback(results.slice(1));}));
+ });
+ };
+ wrapIterator(exports.iterator(arr))();
+};
+
+exports.iterator = function(arr){
+ var makeCallback = function(index){
+ var fn = function(){
+ arr[index].apply(null, arguments);
+ return fn.next();
+ }
+ fn.next = function(){
+ return (index < arr.length-1)? makeCallback(index+1): undefined;
}
+ return fn;
};
- makeCallback(0)();
+ return makeCallback(0);
};
View
49 test/test-async.js
@@ -164,3 +164,52 @@ exports.testSeries = function(test){
test.done();
});
};
+
+exports.testIterator = function(test){
+ var call_order = [];
+ var iterator = async.iterator([
+ function(){call_order.push(1);},
+ function(arg1){
+ test.equals(arg1, 'arg1');
+ call_order.push(2);
+ },
+ function(arg1, arg2){
+ test.equals(arg1, 'arg1');
+ test.equals(arg2, 'arg2');
+ call_order.push(3);
+ },
+ ]);
+ iterator();
+ test.same(call_order, [1]);
+ var iterator2 = iterator();
+ test.same(call_order, [1,1]);
+ var iterator3 = iterator2('arg1');
+ test.same(call_order, [1,1,2]);
+ var iterator4 = iterator3('arg1', 'arg2');
+ test.same(call_order, [1,1,2,3]);
+ test.equals(iterator4, undefined);
+ test.done();
+};
+
+exports.testIteratorNext = function(test){
+ var call_order = [];
+ var iterator = async.iterator([
+ function(){call_order.push(1);},
+ function(arg1){
+ test.equals(arg1, 'arg1');
+ call_order.push(2);
+ },
+ function(arg1, arg2){
+ test.equals(arg1, 'arg1');
+ test.equals(arg2, 'arg2');
+ call_order.push(3);
+ },
+ ]);
+ var fn = iterator.next();
+ var iterator2 = fn('arg1');
+ test.same(call_order, [2]);
+ iterator2('arg1','arg2');
+ test.same(call_order, [2,3]);
+ test.equals(iterator2.next(), undefined);
+ test.done();
+};

0 comments on commit 4930440

Please sign in to comment.
Something went wrong with that request. Please try again.