Should Express JS be able to serve content while awaiting inside an existing request [duplicate]

Console log Image

const express = require('express');
const app = express();
const port = 4444;

app.get('/', async (req, res) => {
  console.log('got request');

  await new Promise(resolve => setTimeout(resolve, 10000));

  console.log('done');
  res.send('Hello World!');
});

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`);
});

If I hit get request http://localhost:4444 three times concurrently then it is returning logs as below

got request
done
got request
done
got request
done

Shouldn't it return the output in the below way because of nodes event loop and callback queues which are external to the process thread? (Maybe I am wrong, but need some understanding on Nodes internals) and external apis in node please find the attached image Javascript Run time environment

got request
got request
got request
done
done
done

Thanks to https://stackoverflow.com/users/5330340/phani-kumar

I got the reason why it is blocking. I was testing this in chrome. I am making get requests from chrome browser and when I tried the same in firefox it is working as expected.

Reason is because of this

Chrome locks the cache and waits to see the result of one request before requesting the same resource again.

Chrome stalls when making multiple requests to same resource?


It is returning the response like this: enter image description here

Node.js is event driven language. To understand the concurrency, you should look a How node is executing this code. Node is a single thread language(but internally it uses multi-thread) which accepts the request as they come. In this case, Node accepts the request and assign a callback for the promise, however, in the meantime while it is waiting for the eventloop to execute the callback, it will accept as many request as it can handle(ex memory, cpu etc.). As there is setTimeout queue in the eventloop all these callback will be register there and once the timer is completed the eventloop will exhaust its queue.

Single Threaded Event Loop Model Processing Steps:

  • Client Send request to the Node.js Server.

  • Node.js internally maintains a limited(configurable) Thread pool to provide services to the Client Requests.

  • Node.js receives those requests and places them into a Queue that is known as “Event Queue”.

  • Node.js internally has a Component, known as “Event Loop”. Why it got this name is that it uses indefinite loop to receive requests and process them.

  • Event Loop uses Single Thread only. It is main heart of Node JS Platform Processing Model.

  • Event Loop checks any Client Request is placed in Event Queue. If not then wait for incoming requests for indefinitely.

  • If yes, then pick up one Client Request from Event Queue

    • Starts process that Client Request

    • If that Client Request Does Not requires any Blocking IO Operations, then process everything, prepare response and send it back to client.

    • If that Client Request requires some Blocking IO Operations like interacting with Database, File System, External Services then it will follow different approach

  • Checks Threads availability from Internal Thread Pool

  • Picks up one Thread and assign this Client Request to that thread.

  • That Thread is responsible for taking that request, process it, perform Blocking IO operations, prepare response and send it back to the Event Loop

You can check here for more details (very well explained).