Microtasks and Promises
Learn why promise callbacks run first.
What Is a Microtask?
A microtask is a high-priority deferred job. Promise callbacks (.then, .catch, .finally) and queueMicrotask all schedule microtasks.
The key rule: after each synchronous run, all microtasks drain before any macrotask.
Promise Callbacks Are Deferred
Even an already-resolved Promise schedules its .then callback as a microtask. It does not run synchronously.
console.log('start');
Promise.resolve().then(() => console.log('promise then'));
console.log('end');All lessons in this course
- The Call Stack
- Macrotasks and the Task Queue
- Microtasks and Promises
- queueMicrotask and Ordering