- JavaScript
- The Event Loop, Microtasks, and Asynchronous Concurrency
- Promises & Chaining
Promises in JavaScript: Chaining & Error Handling Rules
Promises in JavaScript represent the eventual completion or failure of an asynchronous operation; each one exists in one of three states: pending, fulfilled, or rejected, and promise chaining builds on that. Chaining works because every .then() and .catch() returns a new Promise, with unhandled errors propagating downstream until caught.
Key Learning Objectives
Understand Promises/A+ chaining and how every .then() returns a fresh promise.
Master error propagation and how .catch() recovers rejected promises back to fulfilled state.
Learn why .finally() runs unconditionally and passes through the prior settled value.
Trace asynchronous microtask queue scheduling during multi-stage promise reactions.
The Interview Problem
What output is logged to the console when the following promise chain executes, and how do errors propagate through .catch() and .finally()?
1const results = [];23Promise.resolve('start')4 .then((val) => {5 results.push(val);6 throw new Error('fail');7 })8 .then(() => {9 results.push('unreachable');10 })11 .catch((err) => {12 results.push('caught: ' + err.message);13 return 'recovered';14 })15 .finally(() => {16 results.push('cleanup');17 return 'ignored';18 })19 .then((val) => {20 results.push('end: ' + val);21 console.log(results.join(' -> '));22 });
Predict Console Output
Select the option that matches what this code prints to the console:
start -> caught: fail -> cleanup -> end: recovered
start -> caught: fail -> cleanup -> end: ignored
start -> unreachable -> caught: fail -> cleanup -> end: recovered
start -> caught: fail -> end: recovered
V8 Engine Execution Trace
Step 1 of 8 (Line 1)Global script execution begins. An empty results array is allocated in heap memory.
Promises in JavaScript Explained
Promises/A+ Specification & Microtask Lifecycle
- Chaining Returns New Promises: In the Promises/A+ specification, calling
.then()or.catch()does not mutate the original promise; it returns a completely new Promise whose state depends on the outcome of the handler:- If the handler returns a value
x, the returned promise resolves withx. - If the handler throws an error
e, the returned promise rejects withe. - If the handler returns a Promise
P, the returned promise adopts the state ofP.
- If the handler returns a value
- Error Recovery with .catch(): An unhandled error bubbles down the chain, skipping all
.then(onFulfilled)callbacks until anonRejectedhandler (like.catch()) is encountered. If.catch()returns normally (e.g.return 'recovered'), the returned promise transitions back to fulfilled state. Downstream.then()blocks will treat this as a successful resolution. - The .finally() Passthrough Guarantee: Introduced in ES2018,
.finally(callback)is guaranteed to run when the promise settles (either fulfilled or rejected). Key behaviors:- The callback receives zero arguments.
- Any value returned from the callback is ignored; the promise adopts the fulfillment value of the preceding promise (
'recovered'). - Exception: If the
.finally()callback itself throws an exception or returns a rejected promise, the chain rejects with that new error.
Common Traps & Mistakes
Assuming returning from `.catch()` leaves the promise in a rejected state. It recovers the promise to fulfilled unless you re-throw.
Expecting `.finally()` to transform the resolved value like `.then()` does. It cannot modify the value unless it throws.
Forgetting that `.then(onFulfilled, onRejected)` cannot catch errors thrown inside its own `onFulfilled` handler. Only subsequent chained `.catch()` blocks can.
FAANG Follow-Up Probes
Probe #1
What is the difference between `.then(null, onRejected)` and `.catch(onRejected)`?
Probe #2
How does `.finally()` handle an unhandled rejection when the finally callback completes successfully?
Probe #3
How would you implement `Promise.prototype.finally` from scratch using `.then()`?
Promises in JavaScript Interview Questions
Key interview inquiries, edge cases, and architectural follow-ups for Promises & Chaining.
Why does calling .then() always return a brand new Promise?
Returning a new Promise allows chaining sequential asynchronous operations. The new Promise resolves to whatever value or Promise the handler returns, or rejects if the handler throws an error.
How does error propagation work in a Promise chain?
If an error occurs or a Promise rejects, downstream .then() onFulfilled handlers are bypassed until the engine encounters the first .catch() or onRejected handler. If that handler returns a value, the chain recovers to fulfilled.
What is the difference between .then(onSuccess, onError) and .then(onSuccess).catch(onError)?
In .then(onSuccess, onError), the onError handler cannot catch errors thrown inside the adjacent onSuccess handler on the same turn. In .then(onSuccess).catch(onError), the catch handler catches errors from both the original promise and the onSuccess callback.
How does Promise.prototype.finally() behave regarding return values and errors?
.finally() executes unconditionally for cleanup. Its returned value is ignored, passing through the prior fulfilled value. However, if the .finally() callback throws an exception or returns a rejected Promise, the chain rejects with that new error.
What causes an "UnhandledPromiseRejection" error?
An unhandled rejection occurs when a Promise rejects and no .catch() or onRejected handler is attached to the chain before the event loop drains its microtask queue, which may terminate Node.js processes.
More JavaScript interview questions for freshers
