Await inside a loop

data/await-in-loopwarningperformancecorrectness

Why it matters

Each iteration waits for the previous round trip before starting its own: N items cost N sequential database or network latencies on the request path.

Fix

Start the calls together and await them once (await Promise.all(items.map(load))), or fetch them all in one batched query (where: { id: { in: ids } }).

Credit

  • From ESLint

    The no-await-in-loop rule flags awaiting inside a loop because iterations then run one after another.

References

See it