What are the states of a Promise?
A promise is always in one of three states: pending (not finished yet), fulfilled (finished with a value) or rejected (finished with a reason, usually an Error); once it leaves pending it is settled and can never change again.
The states
- Pending: the initial state. The operation is still running.
- Fulfilled:
resolve(value)was called with a non-promise value..thenhandlers receive the value. - Rejected:
reject(reason)was called or the executor threw..catchhandlers receive the reason.
"Settled" is the umbrella term for fulfilled or rejected.
Resolved is not the same as fulfilled
Calling resolve(anotherPromise) makes the promise follow the other one. It is "resolved" (locked in) but still pending until that other promise settles, and it may end up rejected. That is also why returning a promise from .then waits for it.
Immutability of the outcome
- Only the first
resolveorrejectcounts; the rest are ignored. - Handlers added after settlement still run, receiving the stored value.
Inspecting the state
There is no public promise.state property. In tests or debugging you can use Promise.race with a sentinel value, or the browser and Node consoles show <pending>, <fulfilled> or <rejected> when you log a promise.
Why it matters
It explains why a promise can safely be shared and awaited from multiple places: everyone receives the same final result.
Code example
1const pendingForever = new Promise(() => {});2const fulfilled = Promise.resolve(42);3const rejected = Promise.reject(new Error("nope"));4rejected.catch(() => {}); // handle it so it isn't reported as unhandled56// Peek at the state with a race against a sentinel (handy in tests)7async function stateOf(p) {8 const pending = Symbol("pending");9 try {10 return (await Promise.race([p, Promise.resolve(pending)])) === pending ? "pending" : "fulfilled";11 } catch {12 return "rejected";13 }14}15console.log(await stateOf(pendingForever), await stateOf(fulfilled), await stateOf(rejected));16// pending fulfilled rejected1718// Settles only once19const once = new Promise((resolve, reject) => {20 resolve("first");21 resolve("second"); // ignored22 reject(new Error("too late")); // ignored23});24console.log(await once); // first2526// "Resolved" with a promise -> follows it, and can still end up rejected27const follower = new Promise((resolve) => resolve(Promise.reject(new Error("inner failure"))));28try {29 await follower;30} catch (e) {31 console.log("follower rejected:", e.message); // follower rejected: inner failure32}3334// Late handlers still receive the stored value35setTimeout(() => fulfilled.then((v) => console.log("late handler got", v)), 10);36await new Promise((r) => setTimeout(r, 20));
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the JavaScript set.