For AI agents: the complete documentation index is available at https://rslint.rs/llms.txt, the full documentation bundle is available at https://rslint.rs/llms-full.txt, and this page is available as Markdown at https://rslint.rs/rules/eslint/no-await-in-loop.md.
close

no-await-in-loop

Added in v0.1.14

Configuration

rslint.config.ts
import { defineConfig, js } from '@rslint/core';

export default defineConfig([
  js.configs.recommended,
  {
    rules: {
      'no-await-in-loop': 'error',
    },
  },
]);

Rule Details

Disallows await expressions inside loop bodies (for, for-in, for-of, while, do-while). Using await in a loop is usually a sign that the program is not taking full advantage of the parallelization benefits of async/await, as each iteration waits for the previous one to complete. The operations can often be refactored to use Promise.all() instead.

This rule also reports await using declarations that dispose resources on each iteration, including for (await using resource of resources). Loop initializers and iterable expressions are allowed when they run only once, but are reported when they repeat inside an outer loop.

This rule allows await in for-await-of loops, since those are designed to work with async iterables. However, a for-await-of nested inside another loop will be flagged.

Examples of incorrect code for this rule:

async function foo(things) {
  for (const thing of things) {
    await bar(thing);
  }
}

async function foo(things) {
  while (things.length) {
    await bar(things.pop());
  }
}

Examples of correct code for this rule:

async function foo(things) {
  await Promise.all(things.map((thing) => bar(thing)));
}

async function foo(things) {
  for await (const thing of asyncIterable) {
    console.log(thing);
  }
}

Original Documentation