SKY-Q402 · Quality, structure and performance
Await in loop
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
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
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
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
import { sendWelcomeEmail } from "./mail.js"; export async function welcomeAll(users) { for (const user of users) { await sendWelcomeEmail(user); }}Not flagged
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
- Runs with
--qualityon TypeScript and JavaScript files. Findings in test files are dropped. - Finds each
awaitand walks up to the nearest loop inside the same function; anawaitinside a nested callback belongs to that callback. - Skips
whileanddoloops,for awaitloops,for (;;)without a condition, anawaitin the loop header rather than its body, and loops that can leave early withreturn, or abreakorcontinueof that loop. - Skips awaits of
Promise.all()orPromise.allSettled(), and of functions or methods namedsleep,delay,backofforpause. - 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.
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.