ES6+ Modern JavaScript

Promises

A Promise represents the eventual result of an asynchronous operation. Promises solve callback hell, enable clean chaining, and are the foundation for async/await.

What is a Promise in JavaScript?

A Promise is an object representing a value that will be available later, such as the result of a network request. It starts out pending, then becomes either fulfilled (with a value) or rejected (with an error). You react with .then() for success and .catch() for errors, or more readably with async/await.

fetch("/api/user")
  .then((res) => res.json())
  .then((user) => console.log(user.name))
  .catch((err) => console.error(err))
  .finally(() => hideSpinner());

const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const [a, b] = await Promise.all([getA(), getB()]);   // parallel

Promise States

Pending

Initial state — operation not yet complete

Fulfilled

Operation completed successfully — has a value

Rejected

Operation failed — has a reason (error)

Once a Promise settles (fulfilled or rejected), it cannot change state. Both fulfilled and rejected are "settled" states.

Promise Methods

.then(onFulfilled, onRejected?)— React to fulfillment; returns a new Promise
.catch(onRejected)— React to rejection; shorthand for .then(null, fn)
.finally(fn)— Runs regardless of outcome — no value/reason arg
Promise.resolve(value)— Returns a pre-fulfilled Promise
Promise.reject(reason)— Returns a pre-rejected Promise
Promise.all([...])— All fulfill or first rejection
Promise.allSettled([...])— All results (fulfilled + rejected)
Promise.race([...])— First settled (any state) wins
Promise.any([...])— First fulfilled wins; rejects if all reject

Promise Combinators Comparison

Promise.all

All must fulfill. First rejection rejects all.

Use: Parallel API calls where all are required

Promise.allSettled

Waits for all. Returns array of {status, value/reason}.

Use: Batch ops where partial success is OK

Promise.race

First to settle (fulfill OR reject) wins.

Use: Timeout pattern: race against a timer

Promise.any

First to FULFILL wins. Rejects only if all reject.

Use: Try multiple mirrors, use first to respond

Common Pitfalls

Not returning in a .then() handler — next step gets undefined
Always return the value or the next Promise inside .then()
Nested .then() chains (callback hell reloaded)
Flatten chains — return promises from .then() instead of nesting
Not attaching .catch() — unhandled rejections crash Node.js
Every promise chain needs a .catch() at the end
Creating a new Promise wrapping an existing Promise
Pass the existing Promise directly or use .then() to transform it

Interactive Examples

Code Editor

<button id="go">Load data</button>
<pre id="out"></pre>

Preview

Promise lifecycle — step by step

1 / 4
1

Pending → Fulfilled or Rejected

A new Promise starts in 'pending' state. Call resolve(value) to transition to 'fulfilled', or reject(reason) to transition to 'rejected'. The state is permanent — it never changes again.

1 of 4
⚠️Unhandled promise rejections
Always attach a .catch() to every promise chain. In Node.js, unhandled rejections crash the process in recent versions. In browsers, they fire the unhandledrejection window event. Use a global handler during development to catch forgotten .catch() calls.

🎯 Quick Check

What does Promise.all([p1, p2, p3]) do if p2 rejects?

JavaScript Promises: summary

Key points

  • Each .then() returns a new promise, so chains stay flat. Always return inside .then().
  • Promise.all fails fast on the first rejection; Promise.allSettled waits for every result.
  • A promise settles only once; its state never changes after that.
  • Unhandled rejections are errors. End every chain with .catch().

Frequently asked questions

›What is the difference between Promise.all and Promise.allSettled?

Promise.all resolves with all values, or rejects as soon as one promise rejects. Promise.allSettled always waits for every promise and gives you each one's status and value or reason.

›Are promises the same as async/await?

async/await is syntax built on promises. An async function returns a promise, and await waits for one.