Reputation: 4299
There have been more than a few posts on this, but I still can't get my head wrapped around this particular problem.
If I have to call three external sites, Facebook, Twitter, and Instagram, and I launch all three with async parallel, and it hangs up for 20 seconds waiting for Facebook to respond, I'm unsure what happens. Does the call the Twitter start, and Instagram, and they both run and possibly complete before the call to Facebook, or does that entire user thread pause until the next round of the event loop, and another users call go through? It looks like the latter to us. Or does the entire thread grind to a halt and wait?
Per the request, here is a cut down version of the code. Service_requests is an array of functions that call Facebook, Instagram, and Twitter. There was a bunch of other stuff going on in here as well, but the meat of it is pretty simple.
Async.parallel(service_requests, function (err, results)
{
if (err) { next(err); return; }
var articles = [];
for (var i = 0; i < results.length; i++)
{
articles = articles.concat(results[i]);
}
next(null, articles);
});
Upvotes: 1
Views: 84
Reputation: 57928
When you make parallel requests to services they all happen 'at the same time'. So if facebook took 20 seconds to return, twitter took 15 seconds and instagram took 25 seconds, you will have all of your data in 25 seconds from when you initiate all of your requests.
moreover, when node waits for responses from these services it its free to service other requests.
the event loop does not grind to a halt and wait for the responses, it continues to do other work if other work exists.
when all 3 services respond with data the async lib will call your final callback with the results.
Upvotes: 2