Skip to content

asyncFilter ​

The asyncFilter() function filters the elements of a generator according to an asynchronous predicate. It returns an asynchronous generator containing only the elements that satisfy the condition.

Interactive example ​

Syntax ​

Classic signature with type guard ​

typescript
function asyncFilter<
	GenericElement extends unknown,
	GenericOutput extends GenericElement,
>(
	iterator: Iterable<GenericElement> | AsyncIterable<GenericElement>,
	predicate: (
		item: GenericElement, 
		params: AsyncGeneratorFilterParams
	) => item is GenericOutput
): AsyncGenerator<GenericOutput, unknown, unknown>

Classic signature with boolean ​

typescript
function asyncFilter<
	GenericElement extends unknown,
>(
	iterator: Iterable<GenericElement> | AsyncIterable<GenericElement>,
	predicate: (
		item: GenericElement, 
		params: AsyncGeneratorFilterParams
	) => boolean
): AsyncGenerator<GenericElement, unknown, unknown>

Curried signature ​

typescript
function asyncFilter<
	GenericElement extends unknown,
>(
	predicate: (
		item: GenericElement, 
		params: AsyncGeneratorFilterParams
	) => boolean
): (
	iterator: Iterable<GenericElement> | AsyncIterable<GenericElement>
) => AsyncGenerator<GenericElement, unknown, unknown>

Parameters ​

  • iterator: The generator (synchronous or asynchronous) to filter
  • predicate: Asynchronous filtering function that receives:
    • item: The current element
    • params.index: The element index

Return value ​

An AsyncGenerator containing the filtered elements.

See also ​

  • filter - Synchronous version of asyncFilter
  • asyncMap - Transforms with an async function
  • asyncReduce - Reduces with an async function

Sources ​

Released under the MIT license.