Last active
August 18, 2016 22:42
-
-
Save BransonGitomeh/e4c9ea166d2680d57ed8805258322742 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var async = { | |
parallel: function(funcs, complete) { | |
console.log("parallel") | |
var counter = 0 | |
var callback = function(argument) { | |
counter = counter + 1 | |
if (counter == funcs.length) { | |
setTimeout(complete, 0); | |
} | |
} | |
funcs.map(function(func) { | |
func(callback) | |
}) | |
}, | |
series: function(funcs, complete) { | |
console.log("series") | |
var counter = 0 | |
var callback = function(argument) { | |
counter = Number(counter) + Number(1) | |
// console.log("couneting", counter) | |
if (counter >= funcs.length) { | |
setTimeout(complete, 0); | |
} else { | |
funcs[counter](callback) | |
} | |
} | |
funcs[0](callback) | |
}, | |
waterfall: function(funcs, complete) { | |
console.log("series") | |
var counter = 0 | |
var callback = function(argument) { | |
counter = Number(counter) + Number(1) | |
if (counter >= funcs.length) { | |
setTimeout(complete, 0); | |
} else { | |
var result = funcs[counter](argument,callback) | |
} | |
} | |
funcs[0](null,callback) | |
}, | |
waterpool: function(funcs, complete) { | |
console.log(funcs) | |
var counter = 0 | |
var returnValues = {} | |
var caller = function(returnValue){ | |
counter = counter + 1 | |
console.log(counter) | |
returnValues[counter] = returnValue | |
if(counter == funcs.length){ | |
complete(returnValues) | |
} | |
} | |
funcs.map(function(func){ | |
func(caller) | |
}) | |
} | |
} | |
// call all together and when all comeplete, notify | |
async.parallel( | |
[ | |
function(callback) { | |
console.log("1") | |
callback() | |
}, | |
function(callback) { | |
setTimeout(function() { | |
console.log("2") | |
callback() | |
}, 3000); | |
}, | |
function(callback) { | |
console.log("3") | |
callback() | |
} | |
], | |
function(done) { | |
console.log("completed calling them parallel") | |
}) | |
// call all one by one in order and when they all finish, notify | |
async.series( | |
[ | |
function(callback) { | |
console.log("1") | |
callback() | |
}, | |
function(callback) { | |
setTimeout(function() { | |
console.log("2") | |
callback() | |
}, 3000); | |
}, | |
function(callback) { | |
console.log("3") | |
callback() | |
} | |
], | |
function(done) { | |
console.log("completed calling them in series") | |
}) | |
// call all of them in parallel and collect the return values, then provide that result on completing | |
async.waterpool( | |
[ | |
function(callback) { | |
console.log("1") | |
callback("returned from 1") | |
}, | |
function(callback) { | |
setTimeout(function() { | |
console.log("2") | |
callback("returned from 2") | |
}, 3000); | |
}, | |
function(callback) { | |
console.log("3") | |
callback("returned from 3") | |
} | |
], | |
function(results) { | |
console.log(results) | |
console.log("completed calling them waterpool") | |
}) | |
// call all the functions and provide the result of the previous function to the next one. till they are over | |
async.waterfall( | |
[ | |
function(result,callback) { | |
console.log(result,"1") | |
callback("returned from 1") | |
}, | |
function(result,callback) { | |
setTimeout(function() { | |
console.log(result,"2") | |
callback("returned from 2") | |
}, 3000); | |
}, | |
function(result,callback) { | |
console.log(result,"3") | |
callback("returned from 3") | |
} | |
], | |
function(done) { | |
console.log("completed calling them waterlfall") | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment