JavaScript Event Loop: Microtasks vs Macrotasks Explained

🧠 Most JavaScript devs argue over this — and that’s the point 👀 (Even seniors don’t agree immediately) No frameworks. No libraries. Just how JavaScript actually schedules work. 🧩 Output-Based Question (Event Loop: microtasks vs macrotasks) console.log("A"); setTimeout(() => {  console.log("B"); }, 0); Promise.resolve().then(() => {  console.log("C"); }); queueMicrotask(() => {  console.log("D"); }); console.log("E"); ❓ What will be printed — in the correct order? ❌ Don’t run the code 🧠 Think like the JavaScript engine A. A → E → C → D → B B. A → C → D → E → B C. A → E → D → C → B D. A → E → C → B → D 👇 Drop ONE option only (no explanations yet 😄) Why this matters Most developers know: Promises run before setTimeout But many don’t know: queueMicrotask runs before .then Console order ≠ execution intuition One wrong assumption = flaky UI or race bugs When fundamentals aren’t clear: async bugs feel random production issues are hard to reproduce debugging becomes guesswork Strong JavaScript developers don’t memorize outputs. They understand why the engine schedules work this way. 💡 I’ll pin the full breakdown after a few answers. #JavaScript #EventLoop #AsyncJavaScript #JSFundamentals #WebDevelopment #FrontendDeveloper #FullStackDeveloper #CodingInterview #DevCommunity #VibeCode 

  • text

Synchronous tasks: A, E (simple console) Microtask : C, D (Promise in Js are microtask, also queueMicrotask are microtask which are given top priority in JS event loop, as Promise is added first in loop becuase of its first come, it is served first), Macrotask : B (setTimeouts are macrotask in JS event loop) So final answer is: A, E, C, D, B

Sync → Microtasks → Macrotasks Sync logs: A, E Microtasks: C, D Macrotask (setTimeout): B A E C D B

See more comments

To view or add a comment, sign in

Explore content categories