noAwaitInLoop
Summary
Section titled Summary- Diagnostic Category:
lint/nursery/noAwaitInLoop
- This rule doesn’t have a fix.
- The default severity of this rule is information.
- Sources:
- Same as
no-await-in-loop
- Same as
Description
Section titled DescriptionDisallow 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()
.
Examples
Section titled ExamplesInvalid
Section titled Invalidasync 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.
Valid
Section titled Validasync function valid() { await Promise.all(things.map((thing) => asyncWork(thing)))}
How to configure
Section titled How to configure{ "linter": { "rules": { "nursery": { "noAwaitInLoop": "error" } } }}