Skip to content
jshacks.dev
Async

Error Propagation in Promise Chains Explained With Examples

Learn how a rejection travels down a promise chain, which handlers it skips, how catch recovers and where errors get lost, with exact output for each case.

7 min read
Summarize in AI
Flat illustration of a log with a failing line and a wrench, with the JavaScript, TypeScript, Node.js, Vite and npm logos.

A rejection in a promise chain behaves like an exception in synchronous code: it skips everything in its way until something handles it. The detail that trips people is what "in its way" means, because the answer depends on the shape of each then, on what a catch returns, and on whether a promise was returned or merely created. This post follows a rejection through a chain in six cases with exact output, from the plain skip to the places where an error is silently lost, and ends with rules that keep a chain honest. The semantics are the same in Node.js 24 and in every current browser.

The plain skip

JavaScript
Promise.resolve("start")
  .then((value) => {
    console.log("step 1", value);
    throw new Error("step 1 failed");
  })
  .then(() => console.log("step 2"))
  .then(() => console.log("step 3"))
  .catch((error) => console.log("caught:", error.message));
Text
step 1 start
caught: step 1 failed

The throw rejects the promise returned by the first then. The next two then calls have no rejection handler, so each passes the rejection straight on. The catch at the end receives it. Steps 2 and 3 never run, the same as statements after a throw in a function body.

then with two arguments

then(onFulfilled, onRejected) takes a second handler, and it does not catch errors thrown by onFulfilled in the same call.

JavaScript
Promise.resolve()
  .then(
    () => {
      throw new Error("from onFulfilled");
    },
    (error) => console.log("same-call handler:", error.message)
  )
  .catch((error) => console.log("next catch:", error.message));
Text
next catch: from onFulfilled

The second argument handles a rejection of the promise then was called on, not the result of the first argument. That is why .then(a).catch(b) is almost always what you want: catch also covers errors thrown inside a.

catch recovers the chain

A catch handler that returns normally turns the chain back to fulfilled, with its return value.

JavaScript
Promise.reject(new Error("first"))
  .catch((error) => {
    console.log("recovering from", error.message);
    return "fallback";
  })
  .then((value) => console.log("then got", value));
Text
recovering from first
then got fallback

If the catch should not swallow the error, rethrow it.

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

Logging and rethrowing is the standard way to observe a failure in the middle of a chain without hiding it.

Rejection is skipped by then, caught by the next catch only

A chain can have several catch handlers, and each handles only what reaches it.

JavaScript
Promise.resolve()
  .then(() => {
    throw new Error("A");
  })
  .catch((error) => {
    console.log("first catch:", error.message);
    throw new Error("B");
  })
  .then(() => console.log("skipped"))
  .catch((error) => console.log("second catch:", error.message));
Text
first catch: A
second catch: B

A throw inside a catch handler starts a new rejection, which the next catch sees. This is how a chain translates errors: the first catch wraps a low-level error into a domain error and rethrows.

finally is transparent

finally runs for both outcomes, does not receive the value or the reason, and passes the result through unchanged, unless it throws or returns a rejected promise.

JavaScript
Promise.reject(new Error("boom"))
  .finally(() => console.log("cleanup"))
  .catch((error) => console.log("after finally:", error.message));

Promise.resolve("ok")
  .finally(() => console.log("cleanup 2"))
  .then((value) => console.log("value kept:", value));
Text
cleanup
cleanup 2
after finally: boom
value kept: ok

The two chains interleave by microtask, which is why cleanup 2 comes before the first chain's catch. Inside each chain the order is the one you would expect. A finally that throws replaces the original outcome with its own error.

Where errors get lost

An inner promise that is not returned

JavaScript
process.on("unhandledRejection", (reason) => {
  console.log("unhandled:", reason.message);
});

Promise.resolve()
  .then(() => {
    Promise.reject(new Error("inner failure")); // created, not returned
    return "chain finished";
  })
  .then((value) => console.log(value))
  .catch((error) => console.log("never reached:", error.message));
Text
chain finished
unhandled: inner failure

The chain reported success, and the failure surfaced separately as an unhandled rejection. The fix is return Promise.reject(...), or more realistically, return the inner operation, so the chain's catch sees it.

A callback API inside a then

JavaScript
Promise.resolve()
  .then(() => {
    setTimeout(() => {
      throw new Error("thrown in a timer");
    }, 0);
  })
  .catch((error) => console.log("never reached:", error.message));

An error thrown in a later callback is not part of the chain at all. It is an uncaught exception, and in Node.js it ends the process. Wrap such APIs in a promise, so that the error is delivered through reject.

Catching too early

JavaScript
function fetchUser() {
  return Promise.reject(new Error("user not found")).catch(() => null);
}

fetchUser().then((user) => console.log("user is", user));
Text
user is null

The catch inside fetchUser converted failure into a null value, and every caller now has to check for it. Sometimes that is intended. When it is not, let the rejection travel and decide at the place that can act on it.

async functions follow the same rules

await throws the rejection reason at the await point, so try and catch takes the place of .catch.

JavaScript
async function run() {
  try {
    await Promise.reject(new Error("failed"));
    console.log("not reached");
  } catch (error) {
    console.log("caught:", error.message);
    throw new Error("wrapped", { cause: error });
  }
}

try {
  await run();
} catch (error) {
  console.log(error.message, "<-", error.cause.message);
}
Text
caught: failed
wrapped <- failed

The cause option on Error, available since Node.js 16.9 and in current browsers, keeps the original error attached when you wrap it, so a log of the final error still shows the root cause.

Rules that keep a chain honest

  1. Return every promise you create inside a then.

  2. End a chain with a catch, or return it to a caller that does.

  3. Prefer .then(a).catch(b) over .then(a, b).

  4. Rethrow, or wrap with cause, when you cannot actually handle the error.

  5. Do not mix callback APIs into a chain without promisifying them.

  6. Use finally for cleanup and nothing else.

Designing error types for chains

A chain is only as informative as its errors. A message such as "request failed" is no help three layers up, while an error class with a code, a message that names the operation and a cause that carries the original gives the caller something to branch on and the logs something to search. Define a small number of error types for the boundaries of your application, such as NotFoundError and ValidationError, and let catch handlers translate low-level errors into these. Callers can then check error instanceof NotFoundError and decide what to show, without parsing strings.

Preserve the stack of the original. Wrapping an error with the cause option keeps both, and most log tools print the chain of causes. Replacing an error with a fresh one that has no cause throws away the information you most need during an incident.

Where to put the single catch

A chain with a catch after every step is as bad as one with none: the handlers repeat, the failures are logged several times and the real decision about what to do is buried. Prefer one catch at the end of a logical operation, placed where you know what the user-facing consequence is. Intermediate catch handlers are for recovery that really is local, such as a fallback value for an optional step, and for translation, where a low-level error is converted into a domain error before it travels on.

Testing rejections

Test the failure paths with the same care as the success paths. In a test, assert that the promise rejects with the expected type and message, using the test runner's rejection assertions, and avoid the common mistake of a try block with no assertion in the catch, which passes when nothing throws at all. Add a test for the case where the cleanup runs, using finally, and one for the case where the error is translated, since both are easy to break when someone refactors the chain.

A short recap for code review

When you review a promise chain, look for four things: does every promise created in a callback get returned, is there a catch or a caller that has one, does any catch swallow an error it cannot handle, and does any callback API run outside the chain. Those four questions find nearly every lost error in a review of ordinary code.

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.