Skip to content

Files

Latest commit

 

History

History
39 lines (31 loc) · 929 Bytes

no-await-in-loop.md

File metadata and controls

39 lines (31 loc) · 929 Bytes

Pattern: Sequential await in loop body

Issue: -

Description

Using await within loops executes async operations sequentially, preventing potential parallelization. This can lead to significantly slower execution times when the operations could be run concurrently.

Examples

Example of incorrect code:

async function processUsers(users) {
  for (const user of users) {
    const userRecord = await getUserRecord(user);
    await processRecord(userRecord);
  }
}

async function processIds(ids) {
  ids.forEach(async id => {
    await processItem(id);
  });
}

Example of correct code:

async function processUsers(users) {
  const userRecords = await Promise.all(
    users.map(user => getUserRecord(user))
  );
  await Promise.all(userRecords.map(record => processRecord(record)));
}

async function processIds(ids) {
  await Promise.all(ids.map(id => processItem(id)));
}