Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added existing code and tests

  • Loading branch information...
commit 44072b8570ae6a0c199ed4ced4dff728809127cd 1 parent 5d942fd
@caolan authored
Showing with 141 additions and 0 deletions.
  1. +61 −0 lib/async.js
  2. +80 −0 test/test-async.js
View
61 lib/async.js
@@ -0,0 +1,61 @@
+var events = require('events');
+
+
+exports.requires = function(requirements, fn){
+ return {requires: requirements, run: fn};
+};
+
+exports.auto = function(tasks, callback){
+ var keys = Object.keys(tasks);
+ if(!keys.length) return callback(null);
+
+ var completed = [];
+ var emitter = new events.EventEmitter();
+ emitter.addListener('taskComplete', function(){
+ if(completed.length == keys.length){
+ callback(null);
+ }
+ });
+
+ keys.forEach(function(k){
+ var task = tasks[k];
+ if(task instanceof Function){
+ task = {run: task};
+ }
+ var taskEnv = {
+ done: function(){
+ completed.push(k);
+ emitter.emit('taskComplete');
+ }
+ };
+ var requires = task.requires || [];
+ var ready = function(){
+ return requires.reduce(function(a,x){
+ return (a && completed.indexOf(x) != -1);
+ }, true);
+ };
+ if(ready()){
+ task.run(taskEnv);
+ }
+ else {
+ var listener = function(){
+ if(ready()){
+ emitter.removeListener('taskComplete', listener);
+ task.run(taskEnv);
+ }
+ };
+ emitter.addListener('taskComplete', listener);
+ }
+ });
+};
+
+exports.waterfall = function(arr){
+ var i = 0;
+ (function(){
+ if(i < arr.length){
+ var args = Array.prototype.slice.call(arguments);
+ arr[i].apply(null, args.concat(arguments.callee));
+ i++;
+ }
+ })();
+};
View
80 test/test-async.js
@@ -0,0 +1,80 @@
+var async = require('async');
+
+
+exports.testRequires = function(test){
+ var fn = function(){return 'test';};
+ test.same(
+ async.requires(['task1','task2'], fn),
+ {requires: ['task1','task2'], run: fn}
+ );
+ test.done();
+};
+
+exports.testAuto = function(test){
+ var callOrder = [];
+ var testdata = [{test: 'test'}];
+ async.auto({
+ task1: {
+ requires: ['task2'],
+ run: function(task){
+ setTimeout(function(){
+ callOrder.push('task1');
+ task.done();
+ }, 100);
+ }
+ },
+ task2: function(task){
+ setTimeout(function(){
+ callOrder.push('task2');
+ task.done();
+ }, 200);
+ },
+ task3: {
+ requires: ['task2'],
+ run: function(task){
+ callOrder.push('task3');
+ task.done();
+ }
+ },
+ task4: {
+ requires: ['task1', 'task2'],
+ run: function(task){
+ callOrder.push('task4');
+ task.done();
+ }
+ }
+ },
+ function(err){
+ test.same(callOrder, ['task2','task3','task1','task4']);
+ test.done();
+ });
+};
+
+exports.testWaterfall = function(test){
+ test.expect(6);
+ var call_order = [];
+ async.waterfall([
+ function(callback){
+ call_order.push('fn1');
+ process.nextTick(function(){callback('one', 'two');});
+ },
+ function(arg1, arg2, callback){
+ call_order.push('fn2');
+ test.equals(arg1, 'one');
+ test.equals(arg2, 'two');
+ setTimeout(function(){callback(arg1, arg2, 'three');}, 100);
+ },
+ function(arg1, arg2, arg3, callback){
+ call_order.push('fn3');
+ test.equals(arg1, 'one');
+ test.equals(arg2, 'two');
+ test.equals(arg3, 'three');
+ process.nextTick(function(){callback('four');});
+ },
+ function(arg4, callback){
+ call_order.push('fn4');
+ test.same(call_order, ['fn1','fn2','fn3','fn4']);
+ test.done();
+ }
+ ]);
+};
Please sign in to comment.
Something went wrong with that request. Please try again.