hi, i have similar problems like shawn: im requesting a service to return internet-sources for a specific media-type, e.g. video -> amazon.de, amazon.com, imdb, ofdb, etc... then i trigger a service for each source, reading and parsing the source has a duration from 3-30 seconds so there is no chance to aggregate checking the sources into one service, after all i want to display some progress too.
currently the whole logic is already separated out in an object which requests the services and counts to 0 to check if it has finished and then calls back a passed delegate. then some aggregating async functions are called, then components are updated via events, which async callback to return their state. i can factor out as much as possible, it keeps being some nasty code in the controller to glue together these async-callbacks.... theres no exact solution im looking for but some best-practices, design patterns to glue async-stuff or perhaps some of this weird javascript function-prototyping-closuring .... perhaps something like function enqueueing.... function doRequestA function doRequestB function doRequestC function callback Function.prototype.enqueue(QueueIdentifier) doRequestA.enqueue('a', callback); doRequestA.enqueue('a', callback); doRequestA.enqueue('a', callback); doRequestB.enqueue('a', callback); // same queue doRequestC.enqueue('c', callback); // new queue or a more generic queue-impl var q = new Queue( function() { //callback-handler initiating just another queue ... var q2 = new Queue( function() { // and on and on and on ... }); doRequestD.enqueue( q2 ) ... doRequestD.enqueue( q2 ) ... doRequestD.enqueue( q2 ) ... }); } ); doRequestA.enqueue( q ).call( ... ) doRequestB.enqueue( q ).call( ... ) doRequestC.enqueue( q ).call( ... ) but thats not very smart either and leads to exactly the same nested callback-handling... thanks for the good answers and remarks so far, appreciate any further comment. yours, kai