1

I wrote a javascript client-server application where i send 1000 HTTP requests to my server (listening on localhost:5000) and await the response for each:

const SendRequest = async () => {
    const res = await fetch("http://localhost:5000")
    return res
}

for(let i=0;i<1000;i++) {
    SendRequest().then(res => console.log(res.status))
    console.log("request sent")
}

When I executed the above code using nodejs, 1000 "request sent" messages were logged to the console, and then after some time, I saw 1000 responses from the server being logged in quick succession.

From my understanding, Javascript is single threaded, but nodejs can use other threads to handle things like setTimeout and awaiting an HTTP response (ref: What the heck is the event loop anyway? | Philip Roberts | JSConf EU). My system has 6 cores and 12 threads. My question is that how can it await 1000s of such operations at once even if it uses up all the 12 threads?

Here is the server side code:

app.get("/", async (req, res) => {
    console.log("Connection established");

    setTimeout(() => {
        console.log("Sending res")
        res.send("Hello from the server")
    }, 5000)        //send response after waiting for 5 seconds
})

Here, I saw "Connection established" logged 1000 times when I sent the requests from the client and 5 seconds later, "Sending res" was logged 1000 times within a matter of milliseconds and all the 1000 responses sent at once which were received by the client. Again, my question is similar, how did these 1000 setTimeouts, each of 5 seconds run concurrently even if we use all the threads in the system?

Akshay Dagar
  • 13
  • 1
  • 2
  • 5
    Well, yes, the *event loop.* Tasks are scheduled in a queue and handled one by one. – deceze Jun 24 '22 at 07:16
  • 3
    *"My question is that how can it await 1000s of such operations at once even if it uses up all the 12 threads?"* Because it uses event-based I/O, it doesn't have a thread dedicated to each request. (And even if it had a thread dedicated to each request, modern operating systems use preemptive multitasking, so each thread would still get some CPU time.) – T.J. Crowder Jun 24 '22 at 07:20
  • @deceze I want to know how those 1000 setTimeouts are able to run concurrently. They are running concurrently because I see very little delay in "sending res" getting printed (the callback getting executed) – Akshay Dagar Jun 24 '22 at 07:21
  • _"I see very little delay in "sending res" getting printed"_ That's because you're fireing off all those async functions in a loop. That loop isn't waiting for `SendRequest` to finish before logging `"request sent"`. – Cerbrus Jun 24 '22 at 07:22
  • Node does not spin up a thread for every `setTimeout()` – mousetail Jun 24 '22 at 07:24
  • 1
    They're not *concurrent*, they're events happening in very quick succession. While the network request is in flight (sent and the response hasn't returned yet), the Javascript thread is free to handle sending the next request. – deceze Jun 24 '22 at 07:24
  • @AkshayDagar setTimeout doesn't require CPU. Waiting doesn't need CPU. Execution after wait requires CPU. – freakish Jun 24 '22 at 07:24
  • @freakish why did you remove the dupe target? If you think it's not a duplicate, could you _at least_ explain why? – Cerbrus Jun 24 '22 at 07:25
  • @Cerbrus because it is not a duplicate. The OP asks about how concurrency works over a single thread. This has nothing to do with async/await. – freakish Jun 24 '22 at 07:26
  • @freakish - The new dupetarget, on the other hand, is on point. :-) – T.J. Crowder Jun 24 '22 at 07:27
  • _"My question is that how can it await 1000s of such operations at once"_ seems like a fit for that dupe target, though... This question is kinda broad, @freakish. – Cerbrus Jun 24 '22 at 07:27
  • @T.J.Crowder indeed. – freakish Jun 24 '22 at 07:27
  • By your logic you could only run as many programs as cores on any PC. This is clearly not the case. Even if node spun op a thread for every async operation (which it does not) you could still run thousands of waiting threads, since the operating system runs it's own event loop. – mousetail Jun 24 '22 at 07:28

0 Answers0