Skip to content

noAwaitInLoops

¥Summary

¥How to configure

biome.json
{
"linter": {
"rules": {
"performance": {
"noAwaitInLoops": "error"
}
}
}
}

¥Description

禁止在循环中使用 await

¥Disallow await inside loops.

在循环中使用 await 会使异步操作逐个执行,而不是同时执行。这会降低运行速度,并可能导致未处理的错误。相反,将所有 Promise 一起创建,然后使用类似 Promise.all() 的方法同时等待它们完成。

¥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().

¥Examples

¥Invalid

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

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.

¥Valid

async function valid() {
await Promise.all(things.map((thing) => asyncWork(thing)))
}

¥Related links