Skip to content

loopAwaits

Reports using await expressions inside loops.

✅ This rule is included in the performance logical presets.

Using await inside a loop causes each iteration to wait for the previous iteration to complete before starting the next one. This sequential execution can significantly slow down your code when the awaited operations are independent and could be executed in parallel.

Consider collecting promises in an array during the loop and then using Promise.all() to await all of them in parallel. This allows all the asynchronous operations to run concurrently, which is typically much faster than running them one at a time.

async function
function processItems(items: string[]): Promise<void>
processItems
(
items: string[]
items
: string[]) {
for (const
const item: string
item
of
items: string[]
items
) {
await
function fetch(input: string | URL | Request, init?: RequestInit): Promise<Response> (+1 overload)
fetch
(`/api/${
const item: string
item
}`);
}
}
async function
function updateRecords(ids: number[]): Promise<void>
updateRecords
(
ids: number[]
ids
: number[]) {
let
let i: number
i
= 0;
while (
let i: number
i
<
ids: number[]
ids
.
Array<number>.length: number

Gets or sets the length of the array. This is a number one higher than the highest index in the array.

length
) {
await
const database: {
update(id: number): Promise<void>;
}
database
.
function update(id: number): Promise<void>
update
(
ids: number[]
ids
[
let i: number
i
]);
let i: number
i
+= 1;
}
}

This rule is not configurable.

If your asynchronous operations must be executed sequentially you may wish to disable this rule for those specific cases. For example, if each operation depends on the result of the previous one, or if you need to avoid overwhelming a rate-limited API, parallel calls may not be an option.

Made with ❤️‍🔥 around the world by the Flint team and contributors.