← Back to JavaScript
35

Promises in JavaScript: clean chaining and error handling

Master `then`, `catch` and `finally`, compose promises clearly and avoid callback hell when asynchronous flows start to grow.

📘 Theory

Promise states and lifecycle

Every promise starts pending and eventually settles as fulfilled or rejected.

1

While a promise is pending, there is still no final value to work with. Once it settles, the result will never change again.

2

A reliable asynchronous design always accounts for both paths: the useful value and the possible failure.

Chain transformations with intention

Each `then()` receives the result of the previous step, so think in a pipeline.

When a `then()` returns a value, that value flows into the next step. When it returns another promise, the chain waits for it before continuing.

This is why a good chain reads like a sequence of small transformations instead of a stack of nested callbacks.

  • Return either a value or a promise from each `then()`.
  • Avoid nested callbacks when chaining already expresses the order clearly.
  • Use one final `catch()` for global failure handling unless a step needs a local recovery strategy.

Compose independent work in parallel

If two asynchronous tasks do not depend on each other, do not wait for them one by one.

1

`Promise.all` is the cleanest way to parallelize independent tasks and shorten total waiting time.

2

The trade-off is fail-fast behavior: if one promise rejects, the whole combined promise rejects too.

🧪 Learn by doing

Example Guided example: a chain with final cleanup Transform values step by step and close the flow with `finally()` no matter how it ends.

🏁 Challenges

Challenge Challenge: build a promise-based delay helper Create a promise that resolves after a timeout and then consume it with a clean chain.

🧰 Resources

Test

Check your knowledge with a test about JavaScript.

Test for JavaScript

What is this?

I'm Cristian Eslava and I sometimes build websites so both you and I can learn and experiment. culTest

I made this in February 2026 to make learning easier for my students. The idea is to learn web development by practicing and to keep expanding the project with new topics, tests and challenges.

It draws inspiration from MDN, W3Schools, CodePen, Manz and many other web development references. I wanted to combine useful theory, runnable examples, challenges and the testing system I had already built for culTest. culTest

If you liked it, if you didn't, or if you want to get in touch, write to me at cristianeslava@gmail.com