{"record":{"id":"79d21d1e05c210ef","repo":"oxc-project/oxc","slug":"unexpected-await-inside-a-loop","errorCode":null,"errorMessage":"Unexpected `await` inside a loop.","messagePattern":"Unexpected `await` inside a loop\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/eslint/no_await_in_loop.rs","lineNumber":12,"sourceCode":"use oxc_ast::{\n    AstKind,\n    ast::{Expression, Statement, VariableDeclarationKind},\n};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::{GetSpan, Span};\n\nuse crate::{AstNode, context::LintContext, rule::Rule};\n\nfn no_await_in_loop_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Unexpected `await` inside a loop.\")\n        .with_help(\"Collect all promises into an array and use `Promise.all()` to run them in parallel, rather than awaiting each one sequentially inside the loop.\")\n        .with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct NoAwaitInLoop;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// This rule disallows the use of `await` within loop bodies. (for, for-in, for-of, while, do-while).\n    ///\n    /// ### Why is this bad?\n    ///\n    /// It potentially indicates that the async operations are not being effectively parallelized.\n    /// Instead, they are being run in series, which can lead to poorer performance.\n    ///\n    /// ### Examples","sourceCodeStart":1,"sourceCodeEnd":30,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/eslint/no_await_in_loop.rs#L1-L30","documentation":"Diagnostic from the oxlint rule `no-await-in-loop` (crates/oxc_linter/src/rules/eslint/no_await_in_loop.rs). It reports an `await` expression appearing in the body of a `for`, `for-in`, `for-of`, `while`, or `do-while` loop. Awaiting inside a loop serializes iterations, so total time is the sum of all awaited operations instead of running them concurrently; the rule's help text suggests collecting the promises and using `Promise.all()`.","triggerScenarios":"Any AwaitExpression nested inside the body (Statement list) of a ForStatement, ForInStatement, ForOfStatement, WhileStatement, or DoWhileStatement — e.g. `for (const id of ids) { await save(id); }`.","commonSituations":"Batch-processing API requests, saving database rows, or fetching per-key lookups in a loop; code written for correctness first and never parallelized; adopting oxlint's default `restriction`/eslint rule set on existing server code.","solutions":["If iterations are independent, map to promises and run concurrently: `await Promise.all(ids.map(id => save(id)));`.","For bounded concurrency, use a worker-pool pattern (e.g. process chunks with `Promise.all` over slices) or p-limit style batching.","If each iteration genuinely depends on the previous result (sequential requirement is intentional), suppress with `// oxlint-disable-next-line no-await-in-loop` on that line.","Disable the rule per-directory in .oxlintrc.json if the codebase intentionally uses sequential awaits as a pattern (e.g. rate-limit compliance)."],"exampleFix":"// before\nfor (const url of urls) {\n  const res = await fetch(url);\n  results.push(res);\n}\n\n// after\nconst results = await Promise.all(urls.map(url => fetch(url)));","handlingStrategy":"fallback","validationCode":"// Before committing, detect awaits directly inside loop bodies in changed files\nconst loopAwait = /(?:for\\s*\\(|while\\s*\\(|do\\s*\\{)/[Symbol.replace];\n// simplest reliable gate: run oxlint on the diff and treat no-await-in-loop as blocking\nexecSync('oxlint --deny-warnings .');","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Default to Promise.all/map when iterating over independent async work.","Add `// oxlint-disable-next-line no-await-in-loop` only with a comment stating the sequential dependency.","Use a concurrency limiter for rate-limited APIs instead of sequential awaits."],"tags":["lint","async","performance","promise","oxlint"],"backgroundTag":"await-in-loop","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}