How does Node.js handle asynchronous code internally? Explain the event loop
phases.
Node.js uses the event loop to handle async tasks without blocking.
Main phases:
- Timers: Executes callbacks scheduled by setTimeout and setInterval.
- Pending callbacks: Executes I/O callbacks deferred to next iteration.
- Idle, prepare: Internal operations.
- Poll: Retrieves new I/O events; executes I/O callbacks.
- Check: Executes callbacks scheduled by setImmediate.
- Close callbacks: Handles closed connections.
Tasks are processed in this order each loop iteration.