Skip to main content

SKY-Q407 · Quality, structure and performance

Discarded async Array.map result

High

ids.map(async (id) => ...) used as a statement on its own. map with an async callback returns an array of promises; when nothing keeps that array, nothing waits for the work or sees its errors. Skylos reports it on values it can prove are plain arrays.

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

Why it matters

The function returns before the notifications are sent, so a caller that reports "done" is wrong, a serverless handler may be frozen with the work half finished, and a failed send becomes an unhandled rejection that can stop a Node.js process.

What Skylos flags

src/notify.ts · TypeScriptReported on line 5
import { sendNotification } from "./push"; export async function notifyAll(userIds: Iterable<string>): Promise<void> {  const ids = Array.from(userIds);  ids.map(async (id) => {    await sendNotification(id);  });}

What Skylos leaves alone

src/notify.ts · TypeScript
import { sendNotification } from "./push"; export async function notifyAll(userIds: Iterable<string>): Promise<void> {  const ids = Array.from(userIds);  const sends = ids.map(async (id) => {    await sendNotification(id);  });  await Promise.all(sends);}

Why Skylos leaves it alone

The array of promises is kept in sends and awaited. Skylos only reports a map result that is thrown away.

More languages

JavaScript

Flagged

src/warm.js · JavaScriptReported on line 5
import { cache } from "./cache.js"; export async function warmCaches(keys) {  const list = Array.from(keys);  list.map(async (key) => {    await cache.refresh(key);  });}

Not flagged

src/warm.js · JavaScript
import { cache } from "./cache.js"; export async function warmCaches(keys) {  const list = Array.from(keys);  return Promise.all(list.map(async (key) => cache.refresh(key)));}

Why Skylos leaves it alone

The promises are passed to Promise.all and returned, so the caller waits for them and sees any failure.

How Skylos decides

  1. Runs with --quality on .ts, .tsx, .js, .jsx, .mts, .cts, .mjs and .cjs files. Generated and minified files, and files that do not parse, are skipped.
  2. Finds x.map(callback) whose callback is async: an inline async arrow or function, or a name bound once to one. Async generators do not count.
  3. The receiver must provably be a built-in array, as for SKY-Q406: an array literal, a const bound to one, new Array(), Array.from(), Array.of() or an array method chain on these, not changed by other code before the call.
  4. The result must be discarded: the call is a statement on its own (also after void, inside a, b or a ternary or && chain used as a statement), only its .length is read, or it is chained only through .filter(Boolean). Assigning, returning or passing it on counts as using it. await ids.map(...) does not: awaiting an array does not wait for the promises inside it.
  5. Reports HIGH on the line where the call starts.

How to fix it

Await all the promises, or use Promise.allSettled when one failure should not stop the others. To send one at a time, use a for (const id of ids) loop with await.

src/notify.ts · TypeScript · the fix, against the flagged file
import { sendNotification } from "./push"; export async function notifyAll(userIds: Iterable<string>): Promise<void> {  const ids = Array.from(userIds);Removed:   ids.map(async (id) => {Removed:     await sendNotification(id);Removed:   });Added:   await Promise.all(ids.map((id) => sendNotification(id)));}

When it can be wrong

  • A deliberate fire-and-forget, where each callback catches 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 parameter typed string[].

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

  • Put the marker on the line where the call starts.

    TypeScript
    ids.map(async (id) => { // skylos: ignore[SKY-Q407] errors handled inside
  • [tool.skylos] ignore = ["SKY-Q407"] turns the rule off for the project.

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