Skip to content

Lesson 3 of 9 · 21m

Async Errors, Rejections, and the Await Boundary

Where thrown errors go in asynchronous code, how unhandled rejections happen, and why promise composition needs deliberate error ownership.

Most async production incidents are not caused by scheduling order. They are caused by errors escaping through a boundary nobody owned. Synchronous exceptions propagate up the call stack. Promise rejections propagate along promise chains. Mixing the two without understanding the boundary produces swallowed failures, duplicate handling, and crashed workers.

A promise is an error channel

function parseConfig(text) {
  return Promise.resolve()
    .then(() => JSON.parse(text))
    .then(validateConfig)
    .catch(error => {
      throw new Error(`invalid config: ${error.message}`);
    });
}

A throw inside a promise callback becomes a rejection; it does not travel up the synchronous caller's stack. The caller only sees it if the promise is awaited or otherwise observed.

async functions convert everything

async function boot() {
  throw new Error("database unavailable");
}

boot(); // returns a rejected promise; it does not throw synchronously

Inside an async function, synchronous throws, rejected awaits, and returned rejected promises all end in the same rejected result promise. That uniformity is useful — until the caller forgets the result exists.

The three classic leaks

// 1. fire-and-forget with no rejection handler
refreshCache();

// 2. catch that accidentally swallows the real failure
loadUser().catch(() => null);

// 3. parallel work where one rejection is never observed
Promise.all([syncA(), syncB(), syncC()]);

Promise.all rejects as soon as one input rejects, but the other promises still run. If their rejections are never consumed, runtimes may report unhandled rejections depending on timing and hooks. Promise.allSettled is often the honest API for independent operations.

Ownership beats decoration

Decide which layer translates errors. Low-level libraries should reject with useful causes. Application orchestration should catch once, add context, and decide whether the operation is retryable. UI layers should present state, not parse stringified exceptions from five layers down.

async function saveInvoice(input) {
  try {
    await api.saveInvoice(input);
  } catch (error) {
    if (error instanceof TypeError) throw error; // programmer bug, not user input
    throw new Error("Invoice could not be saved", { cause: error });
  }
}

Field notes

  • Attach handlers near creation or intentionally detach responsibility. someAsyncWork().catch(reportAndRethrow) inside a utility often double-reports.
  • unhandledrejection handlers are telemetry, not control flow.
  • finally runs even when a rejection is being propagated; use it for cleanup, not for deciding success.
  • Node treats unhandled rejections as fatal in modern defaults. That is a feature: silent corruption is worse than a noisy restart.

Was this lesson helpful?

Sign in to rate