Skip to content

Instantly share code, notes, and snippets.

@timsavery
Created July 2, 2012 21:37
Show Gist options
  • Save timsavery/3035869 to your computer and use it in GitHub Desktop.
Save timsavery/3035869 to your computer and use it in GitHub Desktop.
function hello(name) {
this.name = name;
this.sayIt = function(callback) {
var self = this;
setTimeout(function() {
callback("Hello " + self.name);
}, Math.random()*1000);
};
};
var async = require('async');
var kevin = new hello('kevin')
, tim = new hello('tim')
, wil = new hello('wil')
, roopa = new hello('roopa')
, rick = new hello('rick');
// console.log('=======================');
// console.log('foreach');
// console.log('=======================');
// async.map([kevin, tim, wil, roopa, rick], function(helloObj, callback) {
// helloObj.sayIt(function(msg) {
// console.log(msg);
// callback();
// });
// }, function() {
// console.log('All done!');
// });
// console.log('=======================');
// console.log('map');
// console.log('=======================');
// async.map([kevin, tim, wil, roopa, rick], function(helloObj, callback) {
// helloObj.sayIt(function(msg) {
// callback(null, msg);
// });
// }, function(err, results) {
// results.forEach(function(result) {
// console.log(result);
// });
// });
console.log();
console.log('=======================');
console.log('waterfall');
console.log('=======================');
console.log();
async.waterfall([
function(callback) {
kevin.sayIt(function(msg) {
callback(null, msg, Math.random());
});
},
function(previousResult, randomnumber, callback) {
console.log(previousResult);
tim.sayIt(function(msg) {
callback(null, msg);
});
},
function(previousResult, callback) {
console.log(previousResult);
wil.sayIt(function(msg) {
callback(null, msg);
});
}
], function(err, result) {
console.log('All done %s!', result);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment