Course contentsShow
← JavaScript
Lesson 1196 of 2,72327. The Event LoopFree lesson

Promise Then Callbacks as Microtasks

See how .then and .catch always queue microtasks, even for resolved promises.

Promise Then Callbacks as Microtasks

What you'll learn: How .then() and .catch() callbacks always queue microtasks, even when the promise is already resolved.

The Core Idea

When you attach a .then() or .catch() callback to a promise, JavaScript always schedules that callback as a microtask—even if the promise has already settled (resolved or rejected). This means the callback never runs immediately in the current synchronous flow; it waits until the call stack clears, but runs before any task queue items.

Why This Matters

You might assume that if a promise is already resolved, its .then() callback would run right away. But JavaScript enforces consistency: promise callbacks are always asynchronous. This prevents subtle timing bugs where code behaves differently depending on whether a promise settled before or after you attached the handler.

const resolved = Promise.resolve(42);

console.log('Start');

resolved.then(value => {
  console.log('Then:', value);
});

console.log('End');

// Output:
// Start
// End
// Then: 42

Even though resolved is already settled when we call .then(), the callback doesn't run until after 'End' prints. The .then() callback is placed in the microtask queue and runs only when the call stack empties.

The Analogy

Think of .then() callbacks like priority mail: no matter when you send them (before or after the promise resolves), they must go through the postal system (microtask queue) rather than being hand-delivered immediately. This keeps delivery order predictable.

Key Takeaway: .then() and .catch() callbacks are always microtasks, guaranteeing they run asynchronously and after the current synchronous code completes, but before any tasks from the task queue.