Sure, you can do it that way, here's some pseudocode:
// 'feeds' is an array of the feed URLs
function grabFeeds(feeds) {
var index;
// We start with the first feed
index = 0;
// Kick off the process
feedWorker();
// Our "go get the next feed" function
function feedWorker() {
var feed;
// Do we have any more?
if (index < feeds.length) {
// Yes, request it and bump our index
// (You could combine these lines, but it's
// clearer to keep them separate)
feed = feeds[index];
++index;
start_feed_download(feed, callback);
}
}
// Our callback function
function callback() {
// ...do something with the result...
// Kick of the next feed (feedWorker is defensive,
// so we don't have to check index here)
feedWorker();
}
}
I don't know the Google Feed API, hence the placeholder start_feed_download
function.
What that does is start the process of grabbing the feeds via the grabFeeds
function, which accepts an array of feeds. grabFeeds
kicks off feedWorker
, which initiates the first feed request, and then returns immediately (almost certainly before the first feed is retrieved). callback
processes the result, then asks feedWorker
to kick off the next feed request (if there is one).
The "magic" here is that feedWorker
and callback
are both closures, so even though grabFeeds
has already returned, the index
and feeds
variables live on (in an object called an "execution context") for as long as anything references the things inside the execution context -- in our case, until callback
and feedWorker
are no longer referenced by Google's stuff.