I'm using Node.js and making multiple Asynchronous calls that I need to handle data with when they all finish. I was nesting them, but this was really inefficient as one wouldn't start until the previous finished. I came up with this, and just wondering if there's anything blatantly wrong with it:
var f = function(){},
actualCallback = function() { /* Do real stuff */ },
callbacks = [f, f, f, f, actualCallback];
aync(function() {
callbacks.shift()();
});
aync(function() {
callbacks.shift()();
});
aync(function() {
callbacks.shift()();
});
aync(function() {
callbacks.shift()();
});
aync(function() {
callbacks.shift()();
});
Can't you define a function that works its way through the array?
Then you call that with your array of callback functions.
Now, if you want to launch all the asynchronous callbacks concurrently, then I don't see the problem in the first place; just start them as necessary. If you need to do a sort of "join" at the end of all of them, however, then you'd have to keep track of the general status:
Use the parallel function from the async library.