Skip to main content

What it flags

An async function that awaits or returns a chain of promise methods containing .then(), such as return fetch(url).then((res) => res.json()).then((user) => user.profile), await load().then(async (rows) => save(await enrich(rows))) or an async arrow whose body is such a chain. The function then mixes two promise styles, and error handling is split between .catch() callbacks and the caller. It stays quiet for chains of only .catch() or .finally(), such as await load().catch(() => undefined), which is a compact fallback; for fire-and-forget statements (load().then(report);); for .then() calls passed to Promise.all and other functions; for .then() inside a nested non-async callback; for a chain on a dynamic import(), such as await import('./m').then((m) => m.x), which picks one export of a lazy module; and for functions that are not async. Three kinds of chain are reported as contextual findings, shown with --include-contextual:
  • a one-step chain, where a single .then() and any .catch() or .finally() handlers each map or probe the value in one expression, such as await fetch(url).then((res) => res.json()), return query.execute().then(fromRow), .then(async (res) => res.arrayBuffer()) or return access(path).then(() => true).catch(() => false). It reads about as clearly as a separate await;
  • a settled value, where a one-step .then() is paired with a longer failure handler and the awaited result is stored, such as const found = await probe().then(() => true, (error) => { log(error); return false; }). Rewriting it with try/catch needs a let;
  • any chain in a test, fixture or test helper, where asserting on a request chain such as request(app).get('/').expect(200).then((res) => { ... }) is a common style.
A chain with a second .then(), an async handler or a handler with more than one statement is reported by default.

How to review

Rewrite the chain as sequential await statements and wrap the steps in try/catch where the old .catch() handled errors:
The change moves code between callbacks, so there is no automatic fix.

Configuration

Set the rule to "off" in loadwise.config.json to drop its findings: