Question

I have been reading a pretty good book on node and I am on the topic of the framework where Node.js is non blocking. I come from a VB background so I am use to seeing code go in sequence. For the code below, with regards to none blocking Asynchronous framework. What is the event here, shouldn't the event loop pick up on the "for" meaning this is the event and going in sequence node should not do anything until i++ = var i?

Reason why I ask is that I am thinking of an SNMP server side application and I just can not get in my head around what node.js will do if I tell it to ping 10 devices. If the 7th IP is offline I would have to wait for the snmp timeout to occur before going to the 8th, is this correct?

var http = require('http'),
  urls = ['shapeshed.com', 'www.bbc.co.uk', 'edition.cnn.com'];

function fetchPage(url) {
  var start = new Date();
  http.get({ host: url }, function(res) {
    console.log("Got response from: " + url);
    console.log('Request took:', new Date() - start, 'ms');
  });
}

for(var i = 0; i < urls.length; i++) {
  fetchPage(urls[i]);
}
Was it helpful?

Solution

Coming from a VB background you have an advantage: VB is event driven too! Have you ever needed to can DoEvents() in your VB code? That's telling VB to run the pending events in the event queue.

The difference is that in VB the events are typically user triggered and UI based, mouse clicks and the like. Node, being primarily server side, has events primarily around I/O.

Your code never gets interrupted or blocked (unless you do it deliberately). In the code snippet above, for example, the call to http.get means "go get this URL, and call this callback when you're done." This kicks off the http request and returns immediately. So your for loop will spin through all your URLs, kicking off all the get operations, and then be finished.

At that point you return from your function, and node goes back to the event loop. Once a request is done, node schedules that's request's callback onto the event loop and the callback will eventually run.

One thing to think about: what if one of the http requests finished before the for loop did? In that case the callback would be scheduled on the event loop. But you're not back to the event loop yet, you're still running your for loop. None of the callbacks will execute until you return from whatever function is currently running.

This kind of "don't do too much in an event handler" advice should sound very familiar to a VB programmer.

OTHER TIPS

No. Asynchronous means that I/O (like a HTTP request) does not block; it is transparently handled on separate threads. The call to http.get returns immediately. Thus, your for loop actually completes (in real time) before a single byte goes over the wire.

In the case of the http module, requests are actually queued in the background for you via the Agent class. By default, node will only open 5 concurrent HTTP requests. You can change this by using a custom Agent.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top