JavaScript Task Queues: Microtasks vs Macrotasks Explained

Microtasks vs Macrotasks — How JavaScript Really Schedules Work Ever wondered why this runs the way it does? 👇 console.log("A"); setTimeout(() => console.log("B"), 0); Promise.resolve().then(() => console.log("C")); console.log("D"); Output: A D C B This isn’t a JavaScript “gotcha” it’s how the JS runtime is designed. Behind the scenes, JavaScript uses two task queues: Microtasks → Promise.then, queueMicrotask Macrotasks → setTimeout, setInterval, UI events 🔑 Key rule After the call stack is empty: 👉 Microtasks are executed first (completely) 👉 Then one macrotask is picked 👉 Repeat That’s why promises feel faster than timers. 💡 Why this matters for frontend devs Prevent UI freezes Avoid async race conditions Write smoother React/Vue apps Debug “why did this run before that?” bugs Understand how browsers actually schedule work I wrote a deep-dive Medium article breaking down: ✔ Why JS needs task queues ✔ Call stack & event loop ✔ Microtasks vs macrotasks (with real examples) ✔ Common mistakes even experienced devs make 📖 Read the full article here: 👉 https://lnkd.in/dQr6dKkP If you’ve ever been confused by async JavaScript this one’s for you. #JavaScript #FrontendDevelopment #WebDevelopment #EventLoop #Promises #Performance #React #BrowserInternals

To view or add a comment, sign in

Explore content categories