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 iterationpreviousOutput: Value returned by the previous iterationnext(value?): Continues the loop and emits a valueexit(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 asyncLoopasyncMap- Transforms elements with an async functionexecute- Consumes a generator
