Facebook Pixel

Promise .then, .catch, and .finally Explained

The three promise handlers. Here is what each does, when it runs, and how chaining works.

Promise .then, .catch, and .finally Explained

.then, .catch, and .finally are the three handlers for consuming a promise. Here is what each does.

.then(onFulfilled, onRejected?)

Runs when the promise fulfills. Receives the value. Returns a new promise.

fetchP("/api") .then((response) => response.json()) .then((data) => console.log(data)); `` You can also pass a second argument (onRejected), but `.catch` is cleaner. ### .catch(onRejected) Runs when the promise rejects (or a `.then` throws). Receives the error. Returns a new promise. ```js fetchP("/api") .then((data) => process(data)) .catch((err) => console.error("Error:", err)); `` `.catch` catches errors from anywhere in the chain above it. ### .finally(onSettled) Runs when the promise settles (fulfilled or rejected). Does not receive the value or error. Returns a new promise (with the same value/error). ```js fetchP("/api") .then((data) => render(data)) .catch((err) => console.error(err)) .finally(() => hideSpinner()); `` Use `.finally` for cleanup (hiding spinners, closing connections). ### Chaining Each handler returns a new promise: ```js fetchP("/api") .then((res) => res.json()) // returns a promise .then((data) => process(data)) // returns a promise .then((result) => render(result)) // returns a promise .catch((err) => handleError(err)) .finally(() => cleanup()); `` If a `.then` callback returns a value, the next `.then` receives it. If it returns a promise, the next `.then` waits for it. ### Error Propagation If a `.then` throws, the error propagates to the next `.catch`: ```js fetchP("/api") .then((data) => { throw new Error("oops"); }) .then((data) => console.log("skipped")) .catch((err) => console.error(err)); // catches the error `` ### Recovering From Errors After a `.catch`, the chain continues with the value returned by `.catch`: ```js fetchP("/api") .then((data) => process(data)) .catch((err) => "fallback") // recover with a fallback value .then((result) => render(result)); // receives "fallback" `` ### The Takeaway `.then` handles fulfillment, `.catch` handles rejection (or thrown errors), `.finally` handles settlement (cleanup). Each returns a new promise, enabling chaining. Errors propagate to the next `.catch`. After `.catch`, the chain continues with the recovered value.

.then runs when the promise fulfills (receives the value). .catch runs when the promise rejects or a .then throws (receives the error). .finally runs when the promise settles, regardless of outcome (no value or error received).

Yes. Each .then (and .catch and .finally) returns a new promise. If the callback returns a value, the new promise fulfills with that value. If it returns a promise, the new promise waits for it. This enables chaining.

If a .then callback throws, the error skips subsequent .then callbacks and propagates to the next .catch. If there is no .catch, the error becomes an unhandled rejection. After .catch, the chain continues with the value returned by .catch.

No. .finally does not receive the value or error. It runs for cleanup (hiding spinners, closing connections). The promise returned by .finally passes through the original value or error.

Yes. Return a value from .catch, and the chain continues with that value: .catch(err => 'fallback').then(result => render(result)). The .then receives 'fallback' instead of the error.

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.