no-await-in-loop
eslint
No autofix
eslint.config.js
export default [
{
rules: {
"no-await-in-loop": "error",
},
},
];async function foo(things) {
const results = [];
for (const thing of things) {
// Bad: each loop iteration is delayed until the entire asynchronous operation completes
results.push(await doAsyncWork(thing));
}
return results;
}
async function bar(things) {
for (const thing of things) {
await using resource = getAsyncResource(thing);
}
}async function foo(things) {
const promises = [];
for (const thing of things) {
// Good: all asynchronous operations are immediately started.
promises.push(doAsyncWork(thing));
}
// Now that all the asynchronous operations are running, here we wait until they all complete.
const results = await Promise.all(promises);
return results;
}Examples from the ESLint documentation.
no-await-in-loop
eslint
export default [
{
rules: {
"no-await-in-loop": "error",
},
},
];no-await-in-loop
eslint plugin · perf
{
"rules": {
"no-await-in-loop": "error"
}
}performance/noAwaitInLoops
performance · since 2.0.0
{
"linter": {
"rules": {
"performance": {
"noAwaitInLoops": "error"
}
}
}
}Require braces around arrow function bodies
Disallow if statements as the only statement in else blocks
Disallow returning values from Promise executor functions
Disallow use of chained assignment expressions
Disallow using Object.assign with an object literal as the first argument and prefer the use of object spread instead
Require or disallow assignment operator shorthand where possible