Docs

Namaste JavaScript — Interview Quick Notes

Ep 15: Asynchronous JS & Event Loop


  • Web APIs (setTimeout, DOM, fetch, localStorage, console) live in the browser — not in the JS engine itself.
  • Flow: callback registered in Web API env → on completion, pushed to Callback Queue → Event Loop moves it to Call Stack when stack is empty.
  • Microtask Queue (Promise callbacks, MutationObserver) has higher priority than the Callback Queue — microtasks run first.
  • All callbacks that come through promises go in Microtask Queue.
  • Starvation: if microtasks keep spawning new microtasks, the Callback Queue never gets a turn.
  • Call Stack ← Event Loop ← Microtask Queue (higher priority) / Callback Queue.