Last active
August 29, 2015 14:05
-
-
Save gs-akhan/4a7fcea1c0e5a3d09697 to your computer and use it in GitHub Desktop.
Simple Async handling
This file contains hidden or 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 uselessAsync = {}; | |
uselessAsync.parallel = function(tasks, callback) { | |
var completed = 0; | |
var finalData = []; | |
var cb = function(err, data, iter) { | |
completed++; | |
finalData[iter] = data; | |
if(completed === tasks.length) { | |
callback(finalData); | |
} | |
}; | |
//Loop through the tasks and exceute them by passing in callback which excutes and checks | |
//for the counter variable if equal to number of tasks then it assumes it to be last | |
//task and then executes the main callback the user gives. | |
for(var i = 0 ; i < tasks.length; i++) { | |
tasks[i]((function(ii) { | |
return function(err, data) { | |
cb(null, data, ii); | |
}; | |
}(i))); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to use