Async & Promisesmedium
How does the event loop work in JavaScript, and what role do async functions play in it?
Explain the event loop mechanism and how async/await functions interact with it.
Answer
The event loop allows JavaScript to perform non-blocking I/O operations despite being single-threaded. When an async function is called, it returns a promise. If the promise is pending, execution continues to the next line without waiting. Once the promise resolves, the corresponding .then() or await expression is pushed to the microtask queue, which gets executed after the current task completes, ensuring that async functions run efficiently without blocking the main thread.