src/content/docs/linter/rules/no-await-in-loops.mdx
import { Tabs, TabItem } from '@astrojs/starlight/components';
<Tabs> <TabItem label="JavaScript (and super languages)" icon="seti:javascript"> ## Summary - Rule available since: `v2.0.0` - Diagnostic Category: [`lint/performance/noAwaitInLoops`](/reference/diagnostics#diagnostic-category) - This rule isn't recommended, so you need to enable it. - This rule doesn't have a fix. - The default severity of this rule is [**information**](/reference/diagnostics#information). - Sources: - Same as [`no-await-in-loop`](https://eslint.org/docs/latest/rules/no-await-in-loop){
"linter": {
"rules": {
"performance": {
"noAwaitInLoops": "error"
}
}
}
}
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();
}
}
async function valid() {
await Promise.all(things.map((thing) => asyncWork(thing)))
}