Sub topic 8
Promises Run as Microtasks
This chapter assumes you know basic Promise usage (then, catch) and have read the previous chapter on the microtask queue. You will see exactly when a promise callback gets queued, and why chained callbacks from different promises take turns.
A Reaction Is Queued When the Promise Settles
When you call .then(callback), you register a reaction. The callback does not run right away. It also doesn't wait in the microtask queue yet. The engine queues a microtask, called a promise job, only once two things are true: the promise has settled (fulfilled or rejected), and the reaction is registered.
There are two cases:
- The promise has already settled when you call
.then. The job is queued immediately. - The promise is still pending. The reaction is stored on the promise. When
resolveorrejectis called later, every stored reaction is queued as a microtask, in registration order.
In both cases the callback runs asynchronously, after the current synchronous code finishes.
The Executor Is Synchronous
A common error is to assume everything inside new Promise(...) is asynchronous. In fact the executor function runs right away, during construction. Only the then callbacks are deferred.
console.log('1');
new Promise((resolve) => {
console.log('2'); // runs synchronously, inside the constructor
resolve();
}).then(() => console.log('4'));
console.log('3');
// Output: 1, 2, 3, 4Pending Promises Wait for Their Trigger
If something else settles the promise later, such as a timer, its reaction can't be queued until then.
const p = new Promise((resolve) => {
setTimeout(() => resolve('done'), 0);
});
p.then((v) => console.log('then:', v));
Promise.resolve().then(() => console.log('already resolved'));
// Output: already resolved, then: doneThe second promise is already fulfilled, so its job is queued at once. The first promise only fulfills inside a timer task, so its reaction runs after that task.
Chains Advance One Step per Microtask
Each .then returns a new promise. That new promise settles when its callback returns, and only then is the next reaction in the chain queued. Because of this, two independent chains interleave:
Promise.resolve()
.then(() => console.log('a1'))
.then(() => console.log('a2'));
Promise.resolve()
.then(() => console.log('b1'))
.then(() => console.log('b2'));
// Output: a1, b1, a2, b2Running a1 fulfills the promise that a2 waits on, which queues a2 behind b1, already in the queue. A chain does not run to the end in one go. It moves forward one microtask at a time.
Rejections Follow the Same Path
catch(fn) is shorthand for then(undefined, fn). A rejection queues reactions just as fulfillment does. When a then has no rejection handler, the rejection passes down the chain one microtask per step until a catch handles it.
Exercise
What does this print, and in what order? setTimeout(() => console.log('t'), 0); const p = new Promise((r) => { console.log('e'); r('v'); }); p.then((v) => console.log(v)); console.log('s');
Show answer
e, s, v, t. The executor logs "e" synchronously. "s" is also synchronous. The promise is already fulfilled when then() is called, so "v" is queued as a microtask and runs as soon as the script finishes. The timer callback "t" is a task, so it runs after the microtask queue is drained.
Exercise
Two chains start from Promise.resolve(). Chain A has three then-callbacks logging a1, a2, a3. Chain B has one then-callback logging b1. What is the output?
Show answer
a1, b1, a2, a3. The first step of each chain is queued at the start. Running a1 queues a2 behind b1. After b1 runs, a2 runs and queues a3.