core-utils
    Preparing search index...

    Function forEachAsyncSequentially

    • Iterates over an array and applies an async function to each item sequentially.

      Usages of this function should be intentional. If it's possible to parallelize the tasks, you should use .map and Promise.all() instead, or you could batch promises together with some limit. Only uses this when order matters or when parallel execution would cause issues (e.g., rate limiting, database transactions).

      Type Parameters

      • T

      Parameters

      • array: T[]

        The array to iterate over.

      • asyncTask: (item: T, index: number) => Promise<unknown>

        The async function to apply to each item.

      Returns Promise<void>

      const users = [{ id: 1 }, { id: 2 }, { id: 3 }];
      await forEachAsyncSequentially(users, async (user) => {
      await sendEmail(user.id);
      });