Skip to content

loopAwaits

Reports using await expressions inside loops.

✅ This rule is included in the performance preset.

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 processItems(items: string[]) {
for (const item of items) {
await fetch(`/api/${item}`);
}
}
async function updateRecords(ids: number[]) {
let i = 0;
while (i < ids.length) {
await database.update(ids[i]);
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 ❤️‍🔥 in Boston by Josh Goldberg and contributors.