Problem Statement
Describe the JavaScript event loop and the difference between microtasks and macrotasks.
Explanation
JavaScript runs on a single thread. Synchronous code executes first; asynchronous callbacks are queued. The event loop pulls tasks (macrotasks) like setTimeout and I/O. After each task, the engine drains the microtask queue (Promises, queueMicrotask) before the next macrotask and before painting. Understanding this ordering explains why Promise callbacks often run before setTimeout(…, 0).
Code Solution
SolutionRead Only
console.log('A');
queueMicrotask(() => console.log('microtask'));
setTimeout(() => console.log('macrotask'), 0);
console.log('B');
// A, B, microtask, macrotaskPractice Sets
This question appears in the following practice sets:
