Mind~G
Javascript

Event Loop

Node.Js is a single threaded - It can do only one thing at a time.

It can handle multiple tasks like (reading a file,database query,network request) without blocking the code because of the event loop.

How Event Loop works?

Code

console.log("1: Start cooking");
 
setTimeout(() => {
    console.log("2: Washing vegetables done");
}, 2000);
 
console.log("3: Continue making soup");
 

Execution

  1. console.log("1: Start cooking"); → Runs immediately.
  2. setTimeout(...) → Node.js says, “Hey timer, remind me after 2 seconds.” It doesn't wait.
  3. console.log("3: Continue making soup"); → Runs immediately.
  4. After 2 seconds → Event Loop checks: “Oh, timer is done!” → Runs the console.log inside setTimeout.

Output will be

1: Start cooking
3: Continue making soup
2: Washing vegetables done

On this page