# Promises: then, catch & chaining

> Master the modern foundation of asynchronous JavaScript. Learn how Promises flatten the pyramid of doom and make background tasks predictable.

Subject: JavaScript | Lesson 32 of 63 | 8 min read
Interactive lesson: https://codewithanimation.in/javascript/promises
Previous: [Callbacks & Callback Hell](https://codewithanimation.in/javascript/callbacks-and-async-history.md) | Next: [async / await](https://codewithanimation.in/javascript/async-await.md)

## The IOU of JavaScript

A **Promise** is an object that represents the eventual completion (or failure) of an asynchronous operation. Think of it like a restaurant buzzer: you place your order, you receive a buzzer (the Promise), and you can continue talking with your friends until the buzzer goes off.

A Promise is always in one of three states:

1. `pending`: The initial state. The background task is still running.

2. `fulfilled`: The task completed successfully.

3. `rejected`: The task failed (e.g., a network error).

## Flattening the Pyramid

Promises were introduced in ES6 (2015) specifically to fix Callback Hell.

Instead of nesting functions deep inside one another, Promises allow you to attach callbacks to the returned object using `.then()` for success and `.catch()` for errors.

This structural change pulls your code back to the left margin and centralizes your error handling.

## The Power of Chaining

The true magic of a Promise is that **every `.then()` returns a brand new Promise**.

This means you can chain them together. If you return a value inside a `.then()`, that exact value is passed down as the argument to the very next `.then()` in the chain.

If an error occurs anywhere in the chain, execution immediately skips down to the nearest `.catch()`, ignoring all the `.then()` blocks in between.

## Common mistake: Forgetting to Return

The most common mistake when chaining Promises is forgetting the `return` keyword inside a `.then()`. If you don't return the next Promise or value, the chain breaks, and the next `.then()` will receive `undefined` instead of the data you expected.

## Tip: Settled is final

Once a Promise transitions from `pending` to either `fulfilled` or `rejected`, it is considered 'settled'. A settled Promise is locked forever. It can never change its state or resolve a second time.

## Try it: The Promise Chain

Click 'Fetch Data' to watch a Promise transition from pending to fulfilled, and see how the resolved value is modified and passed down the `.then()` chain.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/javascript/promises

## Check yourself

### Question 1

What are the three possible states of a Promise?

- A. pending, fulfilled, rejected
- B. waiting, resolved, caught
- C. loading, success, error

Correct answer: A

A Promise starts as `pending`. When it finishes, it settles into either a `fulfilled` state or a `rejected` state.

- Why not B: Incorrect. The official spec names are pending, fulfilled, and rejected.
- Why not C: Incorrect. While those are common UI states, they are not the technical Promise states.

### Question 2

How does a Promise chain handle an error that occurs in the very first `.then()`?

- A. It immediately jumps down to the nearest `.catch()`, skipping the rest of the `.then()` blocks.
- B. It passes the error into the next `.then()` for you to handle.
- C. It crashes the browser.

Correct answer: A

A `.catch()` at the end of a chain acts as a safety net, catching any error that happens anywhere above it in the chain.

- Why not B: Incorrect. Errors bypass `.then()` blocks.
- Why not C: Incorrect. That only happens if you don't have a `.catch()` attached anywhere.

### Question 3

Why is it possible to chain multiple `.then()` methods together?

- A. Because every `.then()` returns a new Promise.
- B. Because `.then()` modifies the original Promise.
- C. Because `.then()` is a global Web API.

Correct answer: A

Because `.then()` always returns a new Promise under the hood, you can continually dot-chain them to create a sequential pipeline of asynchronous tasks.

- Why not B: Incorrect. Promises are immutable once settled.
- Why not C: Incorrect. It is a method on the Promise prototype.

## Remember this

- A Promise represents the future result of an asynchronous operation.
- A Promise has three states: pending, fulfilled, and rejected.
- Once a Promise is settled (fulfilled or rejected), its state is locked forever.
- You attach a `.then()` to handle a successful fulfillment, and a `.catch()` to handle a rejection.
- Every `.then()` returns a new Promise, allowing you to chain them sequentially.
- A single `.catch()` at the end of a chain will catch an error from anywhere in the chain.
