Take this code, typical node js example of a Http server, to which I've added a delay of 5 seconds to simulate some async work taking place somewhere else:
const http = require('http');
const hostname = '127.0.0.1';
const port = 8080;
http.createServer((req, res) => {
setTimeout(()=>{
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello World\n');
},5000);
}).listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
What I would expect is that when I open 5 tabs, let's say with a delay of half a second between opening each, the server should "respond" to each tab more or less with this timings:
t=0s - I open first tab
t=0.5s - I open second tab
t=1s - I open third tab
...
t=5s - First tab stops loading, server has replied
t=5.5s - Second tab stops loading, server has replied
t=6s - Third tab stops loading, server has replied
t=6.5s - Fourth tab stops loading, server has replied
t=7s - Fifth tab stops loading, server has replied
However, the behaviour I'm seeing is the following:
t=0s - I open first tab
t=0.5s - I open second tab
t=1s - I open third tab
...
t=5s - First tab stops loading, server has replied
t=10s - Second tab stops loading, server has replied
t=15s - Third tab stops loading, server has replied
t=20s - Fourth tab stops loading, server has replied
t=25s - Fifth tab stops loading, server has replied
As if the subsequent requests weren't starting to run until the first one has finished. Am I missing something here? I thought the whole point of Node JS was to be able to run async taks from a single thread?