SKY-Q406 · Quality, structure and performance
Async callback passed to forEach
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
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
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
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
- Runs with
--qualityon.ts,.tsx,.js,.jsx,.mts,.cts,.mjsand.cjsfiles. Minified files and generated files (underbuild,dist,generatedorvendorfolders,*.gen.*names, or with an@generatedheader) are skipped, and so is a file that does not parse. - Finds calls written
x.forEach(callback)orx["forEach"](callback). - The callback must be
async: an inlineasyncarrow or function, or a name bound once to one (aconstor anasync functiondeclaration that is never reassigned). Async generators do not count. - The receiver must provably be a built-in array: an array literal, a
constbound to one,new Array(),Array(),Array.from(),Array.of(), or a chain of array methods such as.filter()on one of these. Parameters,letvariables, aliases,Set,Mapand objects with their ownforEachare left alone because their type is not proven. - Gives up when
Array.prototype.forEachis reassigned in the file, or when a receiver that is not an array literal (such as anArray.from()result) is passed to other code before theforEach, since that code could have changed it. - 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.
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 aSet.
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.