1

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]);
}
justZito
  • 569
  • 1
  • 10
  • 19
  • Relevant: http://stackoverflow.com/a/19823583/201952 – josh3736 Mar 09 '14 at 17:05
  • josh that is a good read, but it still does not answer what is in queue is it waiting on the response of the url or is it the for statement. Please keep in mind I am only about a week into node and I am just trying to get an understanding. Hell it took me three days to realize my vars were not getting assigned because of the event loop. The one key thing I have learned is that callbacks are the backbone of node. – justZito Mar 09 '14 at 17:15
  • "to realize my vars were not getting assigned because of the event loop" - I think you're assigning more meaning to the event loop than you should be; it doesn't affect synchronous code at all and certainly won't affect variable assignment. – Aaron Dufour Mar 09 '14 at 17:31
  • Aaron what was happening is that I was reading the contents of a file, putting them in a global string so it can be used anywhere in my code. Where I was going wrong was that I did not place a callback to say the file was read and now the contents from the file I place in my string are done. – justZito Mar 09 '14 at 17:44

2 Answers2

2

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.

Chris Tavares
  • 29,165
  • 4
  • 46
  • 63
  • Chris that was great, using it in comparison of VB was such a big help! – justZito Mar 09 '14 at 17:31
  • What do you mean by "*don't do too much in an event handler*"? Isn't in Node just *everything* done in an event handler? – Bergi Mar 09 '14 at 18:40
  • I mean don't do too much in a single run of an event handler. You don't want to launch in ten minutes of computation - you need to get back to the event loop quickly to maintain responsiveness of your app. Long running stuff needs to be split up across multiple turns of the event loop. – Chris Tavares Mar 09 '14 at 20:42
0

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.

Community
  • 1
  • 1
josh3736
  • 139,160
  • 33
  • 216
  • 263
  • In the code if I console out i after fetchPage(urls[i]); console shows 0,1,2 before any results of the sites, in a vb word it would be the site results, 0 , site results , 1 and so on. This was a great read http://blog.carbonfive.com/2013/10/27/the-javascript-event-loop-explained/ – justZito Mar 09 '14 at 17:24