The simplified order
- Everything in sync
- Microtask queue
- Task queue
Real-world examples
Tasks
setTimeout
, setInterval
, setImmediate
, ...
Microtasks
Promises, queueMicrotask
, MutationObserver
, ...
Everything combined
// Schedules a new microtask
queueMicrotask(() => console.log("microtask"));
// Schedules a new task
setTimeout(() => console.log("task"), 0);
console.log("main");
// Output order: main, microtask, task
Please note that this describes a really, really simplified perfect-world case. If you would like to find out more, I can highly recommend Jake Archibald's blog post on this topic.
Top comments (0)