Skip to content

Instantly share code, notes, and snippets.

@nowelium
Created November 18, 2011 12:33
Show Gist options
  • Save nowelium/1376326 to your computer and use it in GitHub Desktop.
Save nowelium/1376326 to your computer and use it in GitHub Desktop.
async.js helpers
//
// async.js <https://github.com/caolan/async> helpers(utils)
// only work on nodejs
//
var async = require('async');
var ControlFlow = function (){};
ControlFlow.prototype.initialize = function (){
var tasks = {};
Object.defineProperty(tasks, 'length', {
value: 0,
enumerable: false,
writable: true
});
this.tasks = tasks;
};
ControlFlow.prototype.add = function(task){
Array.prototype.push.call(this.tasks, task);
return this;
};
ControlFlow.prototype.addWithKey = function(name, task){
this.tasks[name] = task;
this.tasks.length = this.tasks.length + 1;
return this;
};
ControlFlow.prototype.addAll = function(tasks){
this.tasks = tasks;
return this;
};
//
// [code]
// var parallel = new ControlParallel();
// parallel.addAll(parameters.map(function(param){
// return function(next){
// return next(null, param.id);
// });
// }));
// parallel.run(function(err, results){
// console.log(results.map(function(result){ return result.id }));
// });
//
var ControlParallel = function(){
this.initialize.apply(this, arguments);
};
ControlParallel.prototype = new ControlFlow();
ControlParallel.prototype.run = function(callback){
return async.parallel(this.tasks, callback);
};
//
// [code: withKey]
// var serial = new ControlSerial();
// serial.addWithKey('foo', function(next){
// return next(null, 'foo value');
// });
// serial.addWithKey('bar', function(next){
// return next(null, 'bar value');
// });
// serial.run(function(err, results){
// console.log(results.foo); => foo value
// console.log(results.bar); => bar value
// });
//
// [code: withKey any]
// var users = [{id: 1, name: 'doe'}, {id: 2, name: 'john'}];
// var serial = new ControlSerial();
// users.forEach(function(user){
// serial.addWithKey(user.id, function (next){
// return next(null, { age: 20 });
// });
// });
// serial.run(err, results){
// var agedUsers = users.map(function (user){
// user.age = results[user.id].age;
// return user;
// });
// console.log(agesUsers);
// });
//
var ControlSerial = function (){
this.initialize.apply(this, arguments);
};
ControlSerial.prototype = new ControlFlow();
ControlSerial.prototype.run = function(callback){
return async.series(this.tasks, callback);
};
//
// [code]
// async.reduce([1,2,3], 0, function iterator(memo, value, callback){
// console.log('memo => ' + memo + ', value => ' + value);
// return callback(null, memo + value)
// }, function (e, r){
// console.log('rs => ' + r)
// });
//
// [outputs]
// m => 0, i => 1
// m => 1, i => 2
// m => 3, i => 3
// rs => 6
//
// [code]
// var reduce = new ControlReduce();
// reduce.add(function (param, next){
// return next(null, { key1: param.hoge + ' append foo' });
// });
// reduce.add(function (param, next){
// return next(null, { key2: param.key1 + ' append bar' });
// });
// reduce.runWithValue({ hoge: 'value is hoge'}, function(err, result){
// console.log(result); // => key2: 'value is hoge apped foo append bar
// });
//
var ControlReduce = function (){
this.tasks = [];
};
ControlReduce.prototype.add = function(task){
this.tasks.push(task);
};
ControlReduce.prototype.addAll = function(tasks){
this.tasks = tasks;
};
ControlReduce.prototype.run = function(callback){
return async.waterfall(this.tasks, callback);
};
ControlReduce.prototype.runWithValue = function(firstValue, callback){
// array copy
var copyTasks = Array.prototype.slice.call(this.tasks);
copyTasks.unshift(function(next){
return next(null, firstValue);
});
return async.waterfall(copyTasks, callback);
};
//
// [code]
// var async = require('async-utils.js');
// var serial = async.createSerial();
// serial.add(function(next){
// return next(null);
// });
// serial.run(function(err, results){
// // do something
// });
//
//
async.createParallel = function(){
return new ControlParallel();
};
async.createSerial = function (){
return new ControlSerial();
};
async.createReduce = function (){
return new ControlReduce();
};
module.exports = async;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment