await inside a loop runs the iterations one after another

tx0/ts:await-in-looprecommendedwarningperformance

What it finds

for (const url of urls) { await fetch(url); }: each iteration waits for the previous one, so independent operations run one after another instead of together, as Promise.all would run them.

Often the order matters, for example when each step needs the previous result or the work must be throttled, which is why this rule is recommended rather than on by default. Only loops over a collection or a range are checked, for, for-of and for-in: while loops that await are usually polling or retrying, and so are loops that break or return depending on what they awaited. Only await in the parts of a loop that run on every iteration counts: the body, the condition and the update, not the initialization or the iterable of for-of. Loops written with for await are sequential by design and left alone, and so is await in a function defined inside a loop.

From the rule's source, await_in_loop.rs.

Example from the rule's tests
async function fetchAll(urls: string[], poll: () => Promise<boolean>) {  const results = [];  for (const url of urls) {    results.push(await fetch(url));tx0 reports: this await waits for each iteration  }  for (const url of await list()) {    console.log(url);  }  for (let i = await count(); i < urls.length; i++) {    await fetch(urls[i]);tx0 reports: this await waits for each iteration  }  while (!(await poll())) {    await sleep(10);  }  for (const url of urls) {    if (await fetch(url)) {      break;    }  }  for await (const chunk of stream()) {    await write(chunk);  }  for (const url of urls) {    setTimeout(async () => { await fetch(url); });  }  return results;} declare function list(): Promise<string[]>;declare function count(): Promise<number>;declare function sleep(ms: number): Promise<void>;declare function stream(): AsyncIterable<string>;declare function write(chunk: string): Promise<void>;
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/ts:await-in-loop" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/ts:await-in-loop] reason="..."

tx0 explain tx0/ts:await-in-loop shows the same in the terminal. Found a false positive? Report it.