
Microtask vs Macrotask Explained Simply
Microtask vs Macrotask Explained Simply After understanding the Event Loop, there is one more important concept: π Microtask vs Macrotask This is a very common interview question. Letβs understand it in the simplest way possible. First Understand This When asynchronous tasks finish, they donβt directly go to the Call Stack. They go into queues. But there are TWO types of queues: Macrotask Queue Microtask Queue π¦ What is a Macrotask? Macrotasks are normal async tasks like: setTimeout setInterval setImmediate (Node.js) DOM events Example: setTimeout (() => { console . log ( " Macrotask " ); }, 0 ); setTimeout always goes to the Macrotask Queue . β‘ What is a Microtask? Microtasks are higher priority tasks like: Promises (.then, .catch) async/await queueMicrotask Example: Promise . resolve (). then (() => { console . log ( " Microtask " ); }); Promises go to the Microtask Queue . πββοΈ Which Runs First? π Microtasks ALWAYS run before Macrotasks. Even if setTimeout has 0 delay. π» Example con
Continue reading on Dev.to JavaScript
Opens in a new tab



