Skip to content

feat: add prefer-promise-all lint rule#57

Open
danielchen0 wants to merge 2 commits intomainfrom
danielchen0/prefer-promise-all
Open

feat: add prefer-promise-all lint rule#57
danielchen0 wants to merge 2 commits intomainfrom
danielchen0/prefer-promise-all

Conversation

@danielchen0
Copy link
Collaborator

Summary

  • Adds prefer-promise-all rule that detects sequential await in for...of loops that could be parallelized with Promise.all(items.map(async ...))
  • Uses scope analysis to detect cross-iteration variable dependencies (variables declared outside the loop that are both read and written inside)
  • Skips order-dependent patterns: .push(), .unshift(), .splice() to arrays
  • Skips loops with break/return (early exit depends on sequential evaluation)
  • Skips for await...of (async iterator pattern)
  • Skips await inside nested functions (not actually sequential in the loop)
  • Platform: universal (no platform tag — useful on web + backend)

Heuristics

The rule flags when all of these are true:

  1. Loop is for...of (not for, while, or for await...of)
  2. Loop body contains a direct await (not inside a nested function)
  3. No .push() / .unshift() / .splice() calls (order-dependent accumulation)
  4. No break or return targeting the loop (sequential early-exit)
  5. No variable declared outside the loop that is reassigned inside it (cross-iteration dependency)

Test plan

Scenario Expected
for (const x of items) { await doThing(x); } Warning: use Promise.all
Result added to Set/Map Warning: use Promise.all
Result .push()ed to array No warning (order matters)
Loop has break on condition No warning (early exit)
Loop has return on condition No warning (early exit)
Cross-iteration variable (let prev = ...; prev = await ...) No warning (dependency)
Accumulator (total += await ...) No warning (dependency)
for await...of No warning (async iterator)
No await in loop No warning
await inside nested .map() callback No warning (not sequential in loop)
break inside switch (not targeting loop) Still warns (break doesn't exit loop)

Detects sequential await in for...of loops that could be parallelized with
Promise.all. Smart about order-dependent patterns: skips loops that push to
arrays, have cross-iteration variable dependencies, or use break/return for
early exit. Flags only when parallelization is safe.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant