Skip to content

asyncLoop

The asyncLoop() function creates a custom asynchronous generator using an async loop function. It lets you generate sequences of values lazily with asynchronous operations.

Interactive example

Syntax

typescript
async function asyncLoop<
	GenericRawExitOutput extends any = undefined,
	GenericRawNextOutput extends any = undefined,
>(
	loop: (params: GeneratorLoopParams<GenericRawNextOutput>) => Promise<
		| LoopOutputNextResult<GenericRawNextOutput>
		| LoopOutputNextResult<undefined>
		| LoopOutputExistResult<GenericRawExitOutput>
		| LoopOutputExistResult<undefined>
	>
): AsyncGenerator<
	Exclude<GenericRawExitOutput | GenericRawNextOutput, undefined>,
	unknown,
	unknown
>

Parameters

  • loop: Async function called at each iteration that receives:
    • count: Index of the current iteration
    • previousOutput: Value returned by the previous iteration
    • next(value?): Continues the loop and emits a value
    • exit(value?): Ends the loop and emits an optional final value

Return value

An AsyncGenerator that emits the values passed to next() and exit().

See also

  • loop - Synchronous version of asyncLoop
  • asyncMap - Transforms elements with an async function
  • execute - Consumes a generator

Sources

Released under the MIT license.