Skip to content
jshacks.dev
Async

What Is a Promise? States, Settling and Chaining

Learn what a JavaScript promise is, how it moves from pending to fulfilled or rejected exactly once, and how then and catch build a chain of steps.

6 min read
Summarize in AI
Flat illustration of an editor window full of code, with the JavaScript, npm, Vite, Node.js and TypeScript logos.

A promise is an object that stands in for a value you do not have yet. You get it immediately, the value shows up later, and in between you can attach code that says what to do when it does. That is the entire idea, and almost everything confusing about promises comes from forgetting one of its three rules: a promise is in exactly one of three states, it settles at most once, and then always returns a new promise. This post explains each rule on Node.js 24 with printed output, shows how a chain passes values and errors along, and lists the mistakes that make promises look unpredictable. It is the foundation for async/await, which is syntax over everything shown here.

The three states

A promise starts pending. It then becomes fulfilled with a value or rejected with a reason, and from that moment it never changes. Fulfilled and rejected together are called settled.

JavaScript
const pending = new Promise(() => {});
const fulfilled = Promise.resolve(42);
const rejected = Promise.reject(new Error("no"));
rejected.catch(() => {});

console.log(pending);
console.log(fulfilled);
console.log(rejected);
Text
Promise { <pending> }
Promise { 42 }
Promise { <rejected> Error: no
    at ... }

The first promise never settles, because its executor never calls anything. The second is fulfilled with 42, and the third is rejected. The .catch(() => {}) on the rejected one marks the rejection as handled, which stops Node.js from reporting an unhandled rejection. The stack lines after the error vary by file path, so they are elided.

Settling happens once

The function you pass to new Promise, the executor, receives resolve and reject. Whichever is called first wins, and every later call is ignored.

JavaScript
const promise = new Promise((resolve, reject) => {
  resolve("first");
  resolve("second");
  reject(new Error("too late"));
});

promise.then((value) => console.log("value:", value));
Text
value: first

This is a feature. A promise is a one-way, one-time result, so a consumer can trust that it will see one outcome. It also means a promise is the wrong tool for something that happens repeatedly, such as clicks, which an event listener handles.

Attaching handlers

then takes up to two functions, one for fulfillment and one for rejection. catch(fn) is shorthand for then(undefined, fn), and finally(fn) runs either way without receiving the value.

JavaScript
const ok = Promise.resolve("data");
const bad = Promise.reject(new Error("failed"));

ok.then((value) => console.log("ok:", value));
bad.catch((error) => console.log("bad:", error.message));
bad.finally(() => console.log("cleanup ran")).catch(() => {});

console.log("handlers attached");
Text
handlers attached
ok: data
bad: failed
cleanup ran

The promises were already settled, and the handlers still ran after the synchronous line. Handlers are never called immediately. They are queued as microtasks, so a then callback always runs after the current code finishes, whether the promise was settled yesterday or a microsecond ago. That consistency is what makes promises safer than callbacks that might fire either way.

Then returns a new promise

This is the rule that unlocks chaining. p.then(fn) returns a different promise, whose outcome depends on fn.

  • If fn returns a value, the new promise fulfills with it.

  • If fn returns a promise, the new promise follows that promise.

  • If fn throws, the new promise rejects with the error.

JavaScript
Promise.resolve(1)
  .then((n) => n + 1)
  .then((n) => {
    console.log("got", n);
    return new Promise((resolve) => setTimeout(() => resolve(n * 10), 10));
  })
  .then((n) => {
    console.log("got", n);
    throw new Error("stop here");
  })
  .then(() => console.log("skipped"))
  .catch((error) => console.log("caught:", error.message));
Text
got 2
got 20
caught: stop here

Each step receives the previous step's return value. The step that returned a promise made the chain wait ten milliseconds. The throw skipped the following then and went to the nearest catch. A chain reads top to bottom, and a failure falls through to the first rejection handler below it.

Recovery after catch

A catch returns a promise too. If its handler returns normally, the chain is fulfilled again, so you can recover and continue.

JavaScript
Promise.reject(new Error("network down"))
  .catch((error) => {
    console.log("recovering from", error.message);
    return "cached value";
  })
  .then((value) => console.log("continuing with", value));
Text
recovering from network down
continuing with cached value

If you want the failure to continue past the catch, rethrow inside it. Swallowing errors by accident is the most common promise bug, and it is nearly always a catch that logs and returns nothing.

JavaScript
Promise.reject(new Error("db error"))
  .catch((error) => {
    console.log("logging", error.message);
    throw error;
  })
  .then(() => console.log("not reached"))
  .catch((error) => console.log("still failing:", error.message));
Text
logging db error
still failing: db error

Creating a promise from a callback API

Wrap the callback in the executor, and call resolve or reject from inside it.

JavaScript
function delay(ms, value) {
  return new Promise((resolve) => {
    setTimeout(() => resolve(value), ms);
  });
}

const result = await delay(10, "done");
console.log(result);
Text
done

The executor runs immediately and synchronously when you call new Promise. The work starts at that moment, which is why a promise is not lazy. Creating one begins the operation, and then only decides what happens with the result.

JavaScript
console.log("before");

const p = new Promise((resolve) => {
  console.log("executor runs now");
  resolve();
});

console.log("after");
Text
before
executor runs now
after

Mistakes to avoid

**Forgetting to return inside then.** The next step receives undefined, and the chain does not wait.

JavaScript
Promise.resolve(1)
  .then((n) => {
    new Promise((resolve) => setTimeout(() => resolve(n + 1), 10));
  })
  .then((value) => console.log("got", value));
Text
got undefined

The inner promise was created and abandoned. Add return in front of it.

Nesting instead of chaining. A then inside a then rebuilds the pyramid that promises were meant to remove. Return the inner promise and chain at the same level.

Leaving a rejection unhandled. A rejected promise with no handler makes Node.js print an error and exit with code 1 on current versions. End chains with catch, or await them inside try.

Constructing a promise around a promise. new Promise((resolve) => resolve(fetchData())) is redundant. Return fetchData() directly.

How this connects to async and await

await p is p.then(...) with the rest of the function as the callback. async functions return promises, and a throw inside one becomes a rejection. Everything above applies unchanged, and understanding the states is what lets you read an await correctly. The related methods Promise.all and Promise.race combine several promises into one, and each has its own post, starting with Promise.all.

When promises are the wrong tool

A promise carries one result. For a stream of values over time, such as mouse moves, messages on a socket or chunks of a file, use an event emitter, an async iterator or a stream. And since a promise starts its work when it is created and cannot be cancelled by itself, work that must be abortable should take an AbortSignal.

A mental model that holds up

It helps to think of a promise as a receipt for a value that is being prepared. You hold the receipt right away. You can hand it to other code, store it, or attach instructions to it, and none of that requires the value to exist. When the value is ready, the receipt resolves, and every instruction you attached runs in the order you attached it.

That model explains most of the behavior in this post. The receipt can only be redeemed once, which is why a promise settles once. Instructions attached after the value is ready still run, which is why handlers on a settled promise are called. And attaching an instruction gives you a new receipt for whatever that instruction produces, which is why then returns a new promise. When a chain misbehaves, go step by step and ask what receipt each step produced and what it will be redeemed for. The answer is usually a value you forgot to return, or an error you caught and did not rethrow.

Taggedbeginner
Share

Keep reading

Flat illustration of an editor window full of code, with the Google Chrome, JavaScript and npm logos.
Browser

script Tags: async, defer and type=module

Compare classic, async, defer and module script tags: when each downloads, when each runs, how order is kept and which one to pick for each kind of script.

6 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.