Skip to main content

SKY-Q406 · Quality, structure and performance

Async callback passed to forEach

High

Array.prototype.forEach calls its callback and ignores what it returns. An async callback returns a promise, so forEach starts every call and moves on without waiting for any of them. SKY-Q406 reports an async function passed to forEach on a value Skylos can prove is a plain array.

Languages
TypeScript, JavaScript
Quality gate
counted toward the gate limits (full and diff scans)
Finding type
defect
Catalog name
Async Array.forEach callback
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

Why it matters

The code after the loop runs while the work is still in progress: a sync job reports success before its writes finish, and an error thrown inside the callback never reaches the caller's try/catch.

What Skylos flags

src/orders/sync.ts · TypeScriptReported on line 7
import { db } from "../db";import { pushToWarehouse } from "./warehouse"; export async function syncPendingOrders(orderIds: Iterable<string>): Promise<number> {  const ids = Array.from(orderIds);   ids.forEach(async (id) => {    const order = await db.orders.findById(id);    await pushToWarehouse(order);    await db.orders.markSynced(id);  });   return ids.length;}

What Skylos leaves alone

src/orders/sync.ts · TypeScript
import { db } from "../db";import { pushToWarehouse } from "./warehouse"; async function syncOne(id: string): Promise<void> {  const order = await db.orders.findById(id);  await pushToWarehouse(order);  await db.orders.markSynced(id);} export async function syncPendingOrders(orderIds: Iterable<string>): Promise<number> {  const ids = Array.from(orderIds);  const pending: Promise<void>[] = [];   ids.forEach((id) => {    pending.push(syncOne(id));  });   await Promise.all(pending);  return ids.length;}

Why Skylos leaves it alone

The callback given to forEach is not async. It only starts each job and keeps the promise, and the function then waits for all of them with await Promise.all(pending).

How Skylos decides

  1. Runs with --quality on .ts, .tsx, .js, .jsx, .mts, .cts, .mjs and .cjs files. Minified files and generated files (under build, dist, generated or vendor folders, *.gen.* names, or with an @generated header) are skipped, and so is a file that does not parse.
  2. Finds calls written x.forEach(callback) or x["forEach"](callback).
  3. The callback must be async: an inline async arrow or function, or a name bound once to one (a const or an async function declaration that is never reassigned). Async generators do not count.
  4. The receiver must provably be a built-in array: an array literal, a const bound to one, new Array(), Array(), Array.from(), Array.of(), or a chain of array methods such as .filter() on one of these. Parameters, let variables, aliases, Set, Map and objects with their own forEach are left alone because their type is not proven.
  5. Gives up when Array.prototype.forEach is reassigned in the file, or when a receiver that is not an array literal (such as an Array.from() result) is passed to other code before the forEach, since that code could have changed it.
  6. Reports HIGH on the line where the call expression starts; for a chain split over several lines, that is the chain's first line. The callback's body is not inspected.

How to fix it

To run the jobs together, map them to promises and await them all. To run them one at a time, use for (const id of ids) { await … }; Skylos may then add one LOW SKY-Q402 finding per await in the loop, which is expected for deliberate sequential work.

src/orders/sync.ts · TypeScript · the fix, against the flagged file
import { db } from "../db";import { pushToWarehouse } from "./warehouse"; export async function syncPendingOrders(orderIds: Iterable<string>): Promise<number> {  const ids = Array.from(orderIds); Removed:   ids.forEach(async (id) => {Removed:     const order = await db.orders.findById(id);Removed:     await pushToWarehouse(order);Removed:     await db.orders.markSynced(id);Removed:   });Added:   await Promise.all(Added:     ids.map(async (id) => {Added:       const order = await db.orders.findById(id);Added:       await pushToWarehouse(order);Added:       await db.orders.markSynced(id);Added:     }),Added:   );   return ids.length;}

When it can be wrong

  • A deliberate fire-and-forget, where the callback handles its own errors and nobody needs to wait, is still reported.
  • It misses the same bug on receivers it cannot prove are arrays, such as a function parameter typed string[] or a Set.

Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.

How to silence it

  • On the reported line, where the call starts:

    TypeScript
    ids.forEach(async (id) => { // skylos: ignore[SKY-Q406] errors handled inside
  • [tool.skylos] ignore = ["SKY-Q406"] turns the rule off for the project. A baseline accepts existing findings, but it records the absolute file path and the line, so an entry stops matching when the checkout moves or the line shifts.

From the Skylos rule handbook, Skylos 5.8.0 (commit 5b880b0). All rules.