Filters an array by applying an asynchronous predicate function to each element.

This function is implemented in curried form, first taking an array and then a predicate function. Elements are processed sequentially, ensuring order is maintained.

  const users = [
{ id: 1, name: 'Alice', active: true },
{ id: 2, name: 'Bob', active: false },
{ id: 3, name: 'Charlie', active: true }
];

const activeUsers = await ArrayUtil.asyncFilter(users)(
async (user) => {
// Async validation logic (e.g., API call)
await new Promise(resolve => setTimeout(resolve, 100));
return user.active;
}
);
console.log(activeUsers); // [{ id: 1, name: 'Alice', active: true }, { id: 3, name: 'Charlie', active: true }]
  • Type Parameters

    • Input

      The type of elements in the input array

    Parameters

    • elements: readonly Input[]

      The readonly array to filter

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

    A function that takes a predicate and returns a Promise resolving to the filtered array