I need to call the same asynchronous function multiple times but not till one call is finished (i.e. when the callback function is executed). So my code looks like this:
syncFunc(a, function() {
syncFunc(b, function() {
syncFunc(c, function() {
syncFunc(.....)
});
});
});
Is it now possible to shorten this somehow? My first idea was using a for loop, like:
syncParams = [a, b, c, ...];
for(var i = 0;; i++) {
syncFunc(syncParams[i], function() {
if(i < syncParams.length - 1) {
alert('finished');
break;
} else {
continue;
}
}
}
But obviously this does not work as there's no connection for the continue and break to the loop. My other idea was using an interval to check every second if one async call is finished and then call the next one with the next parameter, but this seems too complicated. Is there any easy way without using some library?
EDIT: Because nobody unterstood the library issue: I am developing a PhoneGap mobile app and with the IDE I'm currently working with it is not that pleasent to include a third party library. Also I was just curious how this would work.