Возьмите этот код, типичный пример node js примера Http-сервера, на который я добавил задержку в 5 секунд, чтобы смоделировать некоторую работу async, происходящую где-то в другом месте:
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}/`);
});
Что я ожидаю, так это то, что когда я открываю 5 вкладок, скажем, с задержкой в полсекунды между открытием каждого, сервер должен "отвечать" на каждую вкладку более или менее с такими таймингами:
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
Однако поведение, которое я вижу, следующее:
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
Как будто последующие запросы не запускались, пока первый не завершился. Я что-то упустил? Я думал, что весь смысл node JS должен был иметь возможность запускать асинхронные сигналы из одного потока?