Course contentsShow
← JavaScript
Lesson 1279 of 2,72329. PromisesFree lesson

The .catch() Method

Learn how .catch() registers a callback to handle rejection, receiving the rejection reason as an argument.

The .catch() Method

You'll learn how to handle Promise rejections cleanly using .catch().

What is .catch()?

When a Promise is rejected, you need a way to handle that failure gracefully. The .catch() method registers a callback function that will be invoked if the Promise enters the "rejected" state. It receives the rejection reason (usually an Error object or message) as its argument.

Think of .catch() like a safety net beneath a trapeze artist—it's there to catch you when things go wrong.

How It Works

When you call .catch(onRejected) on a Promise, you're saying: "If this Promise gets rejected, run this callback." The callback receives whatever value the Promise was rejected with:

const promise = new Promise((resolve, reject) => {
  reject("Something went wrong!");
});

promise.catch((reason) => {
  console.log("Caught error:", reason);
  // Logs: "Caught error: Something went wrong!"
});

Connecting with .then()

Remember that .then() can handle both fulfillment and rejection. The .catch() method is actually just shorthand for .then(null, onRejected)—it skips the success handler and goes straight to error handling:

// These are equivalent:
promise.catch((error) => console.log(error));
promise.then(null, (error) => console.log(error));

Position in the Chain

You typically place .catch() at the end of a Promise chain to catch any rejection that occurred anywhere earlier in the chain:

fetch("/api/data")
  .then((response) => response.json())
  .catch((error) => {
    console.error("Failed to fetch or parse:", error);
  });

Key Takeaway: Use .catch(callback) to handle Promise rejections; the callback receives the rejection reason and runs only when the Promise fails.