The type of elements in the input array
The array of elements to filter.
The asynchronous predicate function
that will be executed for each element. Should return true to include the element, false to exclude it.
Optionaloptions: { concurrency?: number; continueOnError?: boolean } = {}Optional configuration
Optionalconcurrency?: numberMaximum number of concurrent operations
OptionalcontinueOnError?: booleanWhether to continue filtering when a predicate throws. When true, elements that throw errors are excluded from the result.
A Promise that resolves to the filtered array.
// Basic usage
const numbers = [1, 2, 3, 4, 5];
const evens = await asyncFilter(numbers, async (n) => n % 2 === 0);
// Result: [2, 4]
// With API validation
const userIds = [1, 2, 3, 4, 5];
const activeUsers = await asyncFilter(userIds, async (id) => {
const user = await api.getUser(id);
return user.status === 'active';
});
Asynchronously filters an array based on an asynchronous predicate function. Only elements for which the predicate returns
trueare included in the result.The function allows limiting the number of concurrently executed predicates, making it useful for managing concurrency in asynchronous operations (e.g., API checks, database queries).