redline
HighAsyncasync-foreach

async callback in forEach

async callback in forEach. forEach doesn't wait for async callbacks. The code after this runs before they finish, and their errors go nowhere.

This is how the comment reads inline, under the flagged line.

Why it matters

arr.forEach(async ...) fires every callback and returns immediately. Rejections become unhandled and ordering is lost.

What to do

for (const x of xs) await f(x) for sequence, or await Promise.all(xs.map(f)) for parallel.

Examples

Flagged
items.forEach(async (i) => { await save(i); });
Better
for (const i of items) await save(i);

What it catches

It reads a pattern on each line, aware of strings and comments. Languages: TypeScript and JavaScript. Scope: All files. These cases are the rule's own tests: the first set must be flagged and the second must not.

Flagged

xs.forEach(async (x) => { await f(x); });

Left alone

for (const x of xs) await f(x);

Research

`async` callbacks in `forEach`, and `async` Promise executors. The returned promises are dropped, so the caller moves on early and rejections escape.

Turn it off or change it

In a config file for the CLI, or in the API's rules field:

{
  "rules": {
    "disable": [
      "async-foreach"
    ],
    "severity": {
      "async-foreach": "low"
    }
  }
}

Read the source or report a false positive.