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
Execution
- console.log("1: Start cooking"); → Runs immediately.
- setTimeout(...) → Node.js says, “Hey timer, remind me after 2 seconds.” It doesn't wait.
- console.log("3: Continue making soup"); → Runs immediately.
- After 2 seconds → Event Loop checks: “Oh, timer is done!” → Runs the console.log inside setTimeout.
Output will be