Await In Loop (ARCH3030)
Key typescript.async.await-in-loop · Severity info · Category async
Awaiting inside a for/while loop body over an array serializes independent async operations that could run concurrently with Promise.all.
Incorrect
Section titled “Incorrect”async function loadAll(ids: string[]): Promise<User[]> { const users: User[] = []; for (const id of ids) { users.push(await fetchUser(id)); // serial, slow } return users;}Correct
Section titled “Correct”async function loadAll(ids: string[]): Promise<User[]> { return Promise.all(ids.map((id) => fetchUser(id)));}Run independent operations concurrently with Promise.all; await in a loop only when there is a genuine sequential dependency.