asyncReduce
The asyncReduce() function reduces a generator to a single value by applying an asynchronous accumulator function to each element. Asynchronous version of reduce().
Interactive example
Syntax
Classic signature
typescript
function asyncReduce<
GenericElement extends unknown,
GenericReduceFrom extends GeneratorEligibleReduceFromValue,
>(
iterator: Iterable<GenericElement> | AsyncIterable<GenericElement>,
startValue: GenericReduceFrom,
theFunction: (
params: GeneratorReduceFunctionParams<
GenericElement,
GeneratorReduceFromValue<GenericReduceFrom>
>
) => GeneratorReduceExitOrNext<GeneratorReduceFromValue<GenericReduceFrom>>
): Promise<GeneratorReduceFromValue<GenericReduceFrom>>Curried signature
typescript
function asyncReduce<
GenericElement extends unknown,
GenericReduceFrom extends GeneratorEligibleReduceFromValue,
>(
startValue: GenericReduceFrom,
theFunction: (
params: GeneratorReduceFunctionParams<
GenericElement,
GeneratorReduceFromValue<GenericReduceFrom>
>
) => GeneratorReduceExitOrNext<GeneratorReduceFromValue<GenericReduceFrom>>
): (
iterator: Iterable<GenericElement> | AsyncIterable<GenericElement>
) => Promise<GeneratorReduceFromValue<GenericReduceFrom>>Parameters
iterator: The generator (synchronous or asynchronous) to reducestartValue: Initial accumulator value (useDGenerator.reduceFrom()for objects)theFunction: Asynchronous reduction function that receives:element: The current elementindex: The element indexlastValue: The previous accumulated valuenext(value): Continues with a new accumulated valueexit(value): Ends and returns the valuenextWithObject(obj1, obj2): Merges two objects (available iflastValueis an object)
Return value
A Promise that resolves with the final accumulated value.
See also
reduce- Synchronous version of asyncReduceasyncMap- Transforms with an async functionasyncFilter- Filters with an async function
