noAwaitInLoops
¥Summary
-
规则生效日期:
v2.0.0¥Rule available since:
v2.0.0 -
诊断类别:
lint/performance/noAwaitInLoops¥Diagnostic Category:
lint/performance/noAwaitInLoops -
此规则没有修复方案。
¥This rule doesn’t have a fix.
-
此规则的默认严重级别为 information。
¥The default severity of this rule is information.
-
来源:
¥Sources:
-
与
no-await-in-loop相同¥Same as
no-await-in-loop
-
¥How to configure
{ "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
Biome v2.1 中文网 - 粤ICP备13048890号