Permalink
Browse files

Merge https://github.com/allanca/async into allanca

Conflicts:
	dist/async.min.js
  • Loading branch information...
caolan committed Oct 29, 2011
2 parents aa837d7 + 9e116c4 commit 58dca5a32ae1e10c5fe6031353549c54658d80ac
Showing with 54 additions and 11 deletions.
  1. +6 −3 README.md
  2. +1 −1 dist/async.min.js
  3. +11 −7 lib/async.js
  4. +36 −0 test/test-async.js
View
@@ -738,9 +738,10 @@ __Example__
Determines the best order for running functions based on their requirements.
Each function can optionally depend on other functions being completed first,
and each function is run as soon as its requirements are satisfied. If any of
the functions pass and error to their callback, that function will not complete
the functions pass an error to their callback, that function will not complete
(so any other functions depending on it will not run) and the main callback
will be called immediately with the error.
will be called immediately with the error. Functions also receive an object
containing the results of functions on which they depend.
__Arguments__
@@ -764,9 +765,11 @@ __Example__
write_file: ['get_data', 'make_folder', function(callback){
// once there is some data and the directory exists,
// write the data to a file in the directory
callback(null, filename);
}],
email_link: ['write_file', function(callback){
email_link: ['write_file', function(callback, results){
// once the file is written let's email a link to it...
// results.write_file contains the filename returned by write_file.
}]
});
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -361,7 +361,7 @@
return callback(null);
}
var completed = [];
var results = {};
var listeners = [];
var addListener = function (fn) {
@@ -382,8 +382,8 @@
};
addListener(function () {
if (completed.length === keys.length) {
callback(null);
if (_keys(results).length === keys.length) {
callback(null, results);
}
});
@@ -396,24 +396,28 @@
callback = function () {};
}
else {
completed.push(k);
var args = Array.prototype.slice.call(arguments, 1);
if (args.length <= 1) {
args = args[0];
}
results[k] = args;
taskComplete();
}
};
var requires = task.slice(0, Math.abs(task.length - 1)) || [];
var ready = function () {
return _reduce(requires, function (a, x) {
return (a && _indexOf(completed, x) !== -1);
return (a && results.hasOwnProperty(x));
}, true);
};
if (ready()) {
task[task.length - 1](taskCallback);
task[task.length - 1](taskCallback, results);
}
else {
var listener = function () {
if (ready()) {
removeListener(listener);
task[task.length - 1](taskCallback);
task[task.length - 1](taskCallback, results);
}
};
addListener(listener);
View
@@ -32,6 +32,42 @@ exports['auto'] = function(test){
});
};
exports['auto results'] = function(test){
var callOrder = [];
async.auto({
task1: ['task2', function(callback, results){
test.same(results.task2, 'task2');
setTimeout(function(){
callOrder.push('task1');
callback(null, 'task1a', 'task1b');
}, 25);
}],
task2: function(callback){
setTimeout(function(){
callOrder.push('task2');
callback(null, 'task2');
}, 50);
},
task3: ['task2', function(callback, results){
test.same(results.task2, 'task2');
callOrder.push('task3');
callback(null);
}],
task4: ['task1', 'task2', function(callback, results){
test.same(results.task1, ['task1a','task1b']);
test.same(results.task2, 'task2');
callOrder.push('task4');
callback(null, 'task4');
}]
},
function(err, results){
test.same(callOrder, ['task2','task3','task1','task4']);
test.same(results, {task1: ['task1a','task1b'], task2: 'task2', task3: undefined, task4: 'task4'});
test.done();
});
};
exports['auto empty object'] = function(test){
async.auto({}, function(err){
test.done();

0 comments on commit 58dca5a

Please sign in to comment.