Elegant way for multiple asynchronous function calls in Javascript -
i need call same asynchronous function multiple times not till 1 call finished (i.e. when callback function executed). code looks this:
syncfunc(a, function() { syncfunc(b, function() { syncfunc(c, function() { syncfunc(.....) }); }); });
is possible shorten somehow? first idea using loop, like:
syncparams = [a, b, c, ...]; for(var = 0;; i++) { syncfunc(syncparams[i], function() { if(i < syncparams.length - 1) { alert('finished'); break; } else { continue; } } }
but not work there's no connection continue , break loop. other idea using interval check every second if 1 async call finished , call next 1 next parameter, seems complicated. there easy way without using library?
edit: because nobody unterstood library issue: developing phonegap mobile app , ide i'm working not pleasent include third party library. curious how work.
i see want call function different argument each time. async.foreachseries appropriate.
if want call function same argument every time, async.whilst work well. https://github.com/caolan/async/#whilsttest-fn-callback
mentioned @jack, question has answer doing yourself: how synch javascript callbacks?
Comments
Post a Comment