Facebook Pixel
Promise Error Handling With .catch in JavaScript

What does .catch do in JavaScript promises?

The answer

It handles rejections from the original promise and thrown errors from any .then above it. .catch receives the error. After .catch, the chain continues with the returned value (recovery).

More FAQs in Promise Error Handling With .catch in JavaScript

Yes. .catch catches both rejections from the original promise and errors thrown in any .then callback above it. This is why .catch is better than .then's second argument, which only catches rejections from the original promise.

Return a value from .catch. The chain continues as fulfilled with that value: .catch(err => 'fallback').then(result => render(result)). The .then receives 'fallback'.

A promise that rejects but has no .catch (or try/catch with async/await). In Node.js, this can crash the process. In browsers, it is a warning. Always handle errors to avoid unhandled rejections.

Still have questions?

Browse all our FAQs or reach out to our support team

Want to upskill yourself?

Your learning library

Small steps. Big possibilities.

Find your next course. Build something great.

Our courses are taking a Coffee break, but your curiosity shouldn't. Stay engaged with namastedev linkedin, youtube, discord and other resources while you wait.

0

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.

Success

Please Login.