Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

updated auto api, removed requires function

  • Loading branch information...
commit 75d506c34eabf314f34fac8e59ffa991f0fc3660 1 parent 4930440
@caolan authored
Showing with 23 additions and 52 deletions.
  1. +7 −18 lib/async.js
  2. +16 −34 test/test-async.js
View
25 lib/async.js
@@ -1,10 +1,6 @@
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);
@@ -18,30 +14,23 @@ exports.auto = function(tasks, callback){
});
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 task = (tasks[k] instanceof Function)? [tasks[k]]: tasks[k];
+ var taskCallback = function(){
+ completed.push(k);
+ emitter.emit('taskComplete');
};
- var requires = task.requires || [];
+ var requires = task.slice(0, Math.abs(task.length-1)) || [];
var ready = function(){
return requires.reduce(function(a,x){
return (a && completed.indexOf(x) != -1);
}, true);
};
- if(ready()){
- task.run(taskEnv);
- }
+ if(ready()) task[task.length-1](taskCallback);
else {
var listener = function(){
if(ready()){
emitter.removeListener('taskComplete', listener);
- task.run(taskEnv);
+ task[task.length-1](taskCallback);
}
};
emitter.addListener('taskComplete', listener);
View
50 test/test-async.js
@@ -1,48 +1,30 @@
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){
+ task1: ['task2', function(callback){
+ setTimeout(function(){
+ callOrder.push('task1');
+ callback();
+ }, 100);
+ }],
+ task2: function(callback){
setTimeout(function(){
callOrder.push('task2');
- task.done();
+ callback();
}, 200);
},
- task3: {
- requires: ['task2'],
- run: function(task){
- callOrder.push('task3');
- task.done();
- }
- },
- task4: {
- requires: ['task1', 'task2'],
- run: function(task){
- callOrder.push('task4');
- task.done();
- }
- }
+ task3: ['task2', function(callback){
+ callOrder.push('task3');
+ callback();
+ }],
+ task4: ['task1', 'task2', function(callback){
+ callOrder.push('task4');
+ callback();
+ }]
},
function(err){
test.same(callOrder, ['task2','task3','task1','task4']);
Please sign in to comment.
Something went wrong with that request. Please try again.