Skip to main content

SKY-Q402 · Quality, structure and performance

Await in loop

Low

An await inside the body of a for loop, so each iteration waits for the previous one to finish. When the iterations do not depend on each other, the work could run side by side.

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

Why it matters

Loading 200 invoices one at a time at 50 ms each takes ten seconds; the same requests in parallel take a fraction of that. This is a review hint, not a bug: order, rate limits or shared state can make the serial loop the right choice.

What Skylos flags

src/invoices.ts · TypeScriptReported on line 6
import { fetchInvoice, Invoice } from "./api"; export async function loadInvoices(ids: string[]): Promise<Invoice[]> {  const invoices: Invoice[] = [];  for (const id of ids) {    invoices.push(await fetchInvoice(id));  }  return invoices;}

What Skylos leaves alone

src/invoices.ts · TypeScript
import { fetchInvoice, Invoice } from "./api"; export async function findPaidInvoice(ids: string[]): Promise<Invoice | undefined> {  for (const id of ids) {    const invoice = await fetchInvoice(id);    if (invoice.paid) {      return invoice;    }  }  return undefined;}

Why Skylos leaves it alone

The loop stops at the first paid invoice, so it is serial on purpose: fetching every invoice at once would do work the early return avoids. Skylos skips loops that can exit early with return, break or continue.

More languages

JavaScript

Flagged

src/welcome.js · JavaScriptReported on line 5
import { sendWelcomeEmail } from "./mail.js"; export async function welcomeAll(users) {  for (const user of users) {    await sendWelcomeEmail(user);  }}

Not flagged

src/welcome.js · JavaScript
import { sendWelcomeEmail } from "./mail.js"; export async function welcomeAll(users) {  for (let i = 0; i < users.length; i += 50) {    await Promise.all(users.slice(i, i + 50).map(sendWelcomeEmail));  }}

Why Skylos leaves it alone

Each iteration sends a batch of 50 in parallel and waits for the batch, which keeps the mail provider's rate limit. Awaiting Promise.all or Promise.allSettled in a loop is deliberate batching.

How Skylos decides

  1. Runs with --quality on TypeScript and JavaScript files. Findings in test files are dropped.
  2. Finds each await and walks up to the nearest loop inside the same function; an await inside a nested callback belongs to that callback.
  3. Skips while and do loops, for await loops, for (;;) without a condition, an await in the loop header rather than its body, and loops that can leave early with return, or a break or continue of that loop.
  4. Skips awaits of Promise.all() or Promise.allSettled(), and of functions or methods named sleep, delay, backoff or pause.
  5. Reports LOW on the line of the await.

How to fix it

Start all requests, then wait for all of them. For long lists, send them in batches as in the JavaScript example, so you do not overload the API.

src/invoices.ts · TypeScript · the fix, against the flagged file
import { fetchInvoice, Invoice } from "./api"; export async function loadInvoices(ids: string[]): Promise<Invoice[]> {Removed:   const invoices: Invoice[] = [];Removed:   for (const id of ids) {Removed:     invoices.push(await fetchInvoice(id));Removed:   }Removed:   return invoices;Added:   return Promise.all(ids.map((id) => fetchInvoice(id)));}

When it can be wrong

  • Work that must run in order, such as database migrations or writes that depend on the previous one, is reported. Keep the loop and suppress the finding.

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 of the await.

    TypeScript
    await runMigration(step); // skylos: ignore[SKY-Q402] migrations run in order
  • [tool.skylos] ignore = ["SKY-Q402"] turns the rule off for the project.

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