asyncmedium
How does JavaScript run asynchronous code if it only has one thread?
Answer
The engine runs one call stack. Anything asynchronous is handed to the host (browser or Node), which does the work elsewhere and queues a callback when it finishes. Once the stack empties, the event loop drains the microtask queue first (promise callbacks, queueMicrotask), then takes one macrotask (setTimeout, I/O, events) and repeats. So JavaScript never blocks on the waiting — it blocks only while your synchronous code is on the stack.
Points an interviewer listens for
- Single call stack; the host handles the actual waiting
- Microtasks (promises) drain fully before the next macrotask
- setTimeout(fn, 0) still waits for the stack to clear
- Long synchronous work freezes the UI — that's what blocks
Reported at
Similar questions have been reported by candidates at HCLTech, Kofuku Idea Labs, Physicswallah, Teksversity, Accenture, AlgoLeap Technologies.
Now try answering it out loud
Interview practice mode times your spoken answer and checks it against the points above.
Practise this question