Why settimeout blocks eventloop

Note. This is not a replicated message for those about setting up, the key answer here is browser design options.

I am starting to learn node.js: A simple example for checking async:

var http=require('http'); http.createServer( function(request, response){ response.writeHead(200); response.write("Hello, dog is running"); setTimeout( function(){ response.write("Dog is done"); response.end(); }, 10000 ); } ).listen(8080); console.log("Listen on port 8080") 

Interestingly, its behavior is different from that in the lind command with curl and in the browser: In Ubuntu 12.10, I use curl localhost: 8080 in two consoles, they respond to almost 10 messages.

However, I open two browsers, make a request almost at the same time, but the whole procedure took me 20 seconds?

thanks.

+6
source share
1 answer

Browser expected, not node.js

If you start the server and request http://localhost:8080/ in two tabs, it will take 20 seconds because the browser waits for the first request to the same URL before starting the second.

If you start the server and request http://localhost:8080/1 and http://localhost:8080/2 on two tabs, it will take another 10 seconds.

+19
source

All Articles