Rejection propagation and finally()
How it works
catch(handler) is equivalent to then(undefined, handler). A catch may recover by returning a value or propagate failure by throwing or returning a rejected promise. finally(cleanup) runs after either settlement and normally preserves the preceding value or reason. If cleanup throws or returns a rejected promise, its failure replaces the original outcome. Keep cleanup focused and avoid hiding the primary failure.
Example 1
Promise.reject(new Error("missing")).catch(() => 5).finally(() => console.log("cleanup")).then(console.log);Expected output in the tested environment:
cleanup5Recovery yields five; normal cleanup preserves it.
Example 2
Promise.resolve(8).finally(() => { throw new Error("cleanup failed"); }).catch(e => console.log(e.message));Expected output in the tested environment:
cleanup failedA throwing cleanup rejects the downstream promise.
Common mistake
Returning a plain value from finally does not substitute that value for the preceding fulfilled result.
Practice
Compare catch returning a fallback with catch rethrowing the error; add a final cleanup log to both.
Run this practice in the documented local environment. ByteMind module and asynchronous grader support is not yet verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Rejection propagation and finally() — Knowledge Check
Reason about the concept and predict its behavior in the indicated environment.
How can catch convert a rejection into fulfillment?
Does a successful finally callback receive the previous fulfillment value as its argument?
What if finally returns a rejected promise?
For Rejection propagation and finally(), what is the console output of example 1 in Node.js 22.16.0?
Promise.reject(new Error("missing")).catch(() => 5).finally(() => console.log("cleanup")).then(console.log);For Rejection propagation and finally(), what is the console output of example 2 in Node.js 22.16.0?
Promise.resolve(8).finally(() => { throw new Error("cleanup failed"); }).catch(e => console.log(e.message));