The type of elements in the input array
A Promise resolving to the filtered array
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 }]
Filters an array by applying an asynchronous predicate function to each element.
Elements are processed sequentially, ensuring order is maintained. The predicate function receives the element, index, and the full array as parameters.