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()]); // parallelPromise States
Initial state — operation not yet complete
Operation completed successfully — has a value
Operation failed — has a reason (error)
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 argPromise.resolve(value)— Returns a pre-fulfilled PromisePromise.reject(reason)— Returns a pre-rejected PromisePromise.all([...])— All fulfill or first rejectionPromise.allSettled([...])— All results (fulfilled + rejected)Promise.race([...])— First settled (any state) winsPromise.any([...])— First fulfilled wins; rejects if all rejectPromise Combinators Comparison
Promise.allAll must fulfill. First rejection rejects all.
Use: Parallel API calls where all are required
Promise.allSettledWaits for all. Returns array of {status, value/reason}.
Use: Batch ops where partial success is OK
Promise.raceFirst to settle (fulfill OR reject) wins.
Use: Timeout pattern: race against a timer
Promise.anyFirst to FULFILL wins. Rejects only if all reject.
Use: Try multiple mirrors, use first to respond
Common Pitfalls
Interactive Examples
Code Editor
<button id="go">Load data</button>
<pre id="out"></pre>Preview
Promise lifecycle — step by step
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.
🎯 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. Alwaysreturninside.then(). Promise.allfails fast on the first rejection;Promise.allSettledwaits 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.