What's the idiomatic way to do the following in Javascript (jQuery)?
- Spawn a set of asynchronous jobs
- Collect partial results
- When every job has completed, combine partial results
The above can be achieved by something on the lines of (assuming for simplicity that requests are processed in order; in a more realistic case, a counter would do the job):
var results = new Array();
$.each(objs, function(i,obj)){
$.getJSON(make_async_request(obj), function(data) {
results[data.key] = data.value;
if (i == objs.length-1)
elaborate(results);
}
});
Which looks ugly to me. Any suggestions?