Skip to content

noAwaitInLoop

Disallow await inside loops.

Using await in a loop makes your asynchronous operations run one after another instead of all at once. This can slow things down and might cause unhandled errors. Instead, create all the promises together and then wait for them simultaneously using methods like Promise.all().

async function invalid() {
for (const thing of things) {
const result = await asyncWork();
}
}
code-block.js:3:24 lint/nursery/noAwaitInLoop ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Avoid using await inside loops.

1 │ async function invalid() {
2 │ for (const thing of things) {
> 3 │ const result = await asyncWork();
^^^^^^^^^^^^^^^^^
4 │ }
5 │ }

Using await inside loops might cause performance issues or unintended sequential execution, consider use Promise.all() instead.

async function valid() {
await Promise.all(things.map((thing) => asyncWork(thing)))
}
biome.json
{
"linter": {
"rules": {
"nursery": {
"noAwaitInLoop": "error"
}
}
}
}