Core concepts Flashcards
What are microtasks and macrotasks?
Macrotasks and microtasks are tasks that are put onto the queue in the event loop. There’s a FIFO queue in the event loop that occurs when the engines is busy executing a synchronous task.
Macrotasks include loading external scripts, dispatching events, handling events, and setTimeout callbacks.
Microtasks include handling promise callbacks(.then, .catch)
The difference is that in one loop,
Immediately after every macrotask, the engine executes all tasks from microtask queue, prior to running any other macrotasks or rendering or anything else.
This is so that all microtasks are completed before any other event handling or rendering or any other macrotask takes place. That’s important, as it guarantees that the application environment is basically the same (no mouse coordinate changes, no new network data, etc) between microtasks.