Transforms each element of an array using an asynchronous function to create a new array.

Similar to JavaScript's native map but processes asynchronous functions sequentially. Each element's transformation is completed before proceeding to the next element, ensuring order is maintained.

  const userIds = [1, 2, 3, 4, 5];

const userDetails = await ArrayUtil.asyncMap(userIds)(
async (id, index) => {
console.log(`Fetching user ${id} (${index + 1}/${userIds.length})`);
const response = await fetch(`/api/users/${id}`);
return await response.json();
}
);
console.log('All users fetched:', userDetails);
  • Type Parameters

    • Input

      The type of elements in the input array

    Parameters

    • elements: readonly Input[]

      The readonly array to transform

    Returns <Output>(
        closure: (
            elem: Input,
            index: number,
            array: readonly Input[],
        ) => Promise<Output>,
    ) => Promise<Output[]>

    A function that takes a transformation function and returns a Promise resolving to the transformed array