Asynchronous JavaScript
35 min read
Promisesasync/awaitEvent LoopMicrotasksMacrotasksCall Stack
Promises, Async/Await & The Event Loop
JavaScript is single-threaded but non-blocking. Understanding the Event Loop is crucial to writing performant, bug-free async code.
The Event Loop
The event loop continuously checks: Is the call stack empty? If yes, take the next task from the queue. There are two queues:
- Microtask Queue (higher priority): Promises, MutationObserver
- Macrotask Queue (lower priority): setTimeout, setInterval, I/O
Promises & Async/Await
Promises represent a future value. async/await is syntactic sugar over Promises that makes async code read like synchronous code.