Skip to content

asyncMap ​

The asyncMap() function transforms each element of a generator by applying an asynchronous transformation function. It returns an asynchronous generator with the transformed values.

Interactive example ​

Syntax ​

Classic signature ​

typescript
function asyncMap<
	const GenericInput extends unknown,
	const GenericOutput extends unknown,
>(
	iterator: AsyncIterable<GenericInput> | Iterable<GenericInput>,
	theFunction: (arg: GenericInput, params: AsyncGeneratorMapParams) => Promise<GenericOutput>
): AsyncGenerator<GenericOutput, unknown, unknown>

Curried signature ​

typescript
function asyncMap<
	const GenericInput extends unknown,
	const GenericOutput extends unknown,
>(
	theFunction: (arg: GenericInput, params: AsyncGeneratorMapParams) => Promise<GenericOutput>
): (iterator: AsyncIterable<GenericInput> | Iterable<GenericInput>) => AsyncGenerator<GenericOutput, unknown, unknown>

Parameters ​

  • iterator: The generator (synchronous or asynchronous) to transform
  • theFunction: Asynchronous transformation function that receives:
    • arg: The current element
    • params.index: The element index

Return value ​

An AsyncGenerator emitting the transformed values.

See also ​

  • map - Synchronous version of asyncMap
  • asyncFilter - Filters with an async function
  • asyncReduce - Reduces with an async function

Sources ​

Released under the MIT license.