Skip to main content

Function: reduceAsync()

Returns the result of reducing the asyncIterable using asyncReducer.

Informally, an initial accumulator is created using RawAsyncReducerWithoutFinish.create. Then each value in asyncIterable is added to the accumulator and the current accumulator is updated using RawAsyncReducerWithoutFinish.add. Finally, the resulting accumulator is transformed using RawAsyncReducerWithFinish.finish if specified. Multiple accumulators may be created, added to, and then combined if supported via RawAsyncReducerWithoutFinish.combine and the next value of asyncIterable is ready before promises from RawAsyncReducerWithoutFinish.add resolve.

If asyncReducer is an async optional reducer (no RawAsyncReducerWithoutFinish.create method), then an empty async iterable is returned if asyncIterable is empty. Otherwise, an async iterable containing the result of reducing using the first value of the async iterable as the initial accumulator is returned.

Like Array.prototype.reduce, but for async iterables.

Example

console.log(
await pipe(
asAsync([`Hello`, `World!`, `What`, `an`, `interesting`, `program!`]),
reduceAsync((a, b) => `${a} ${b}`),
getAsync,
),
)
//=> Hello World! What an interesting program!

console.log(
await pipe(
asAsync([`Hello`, `World!`, `What`, `an`, `interesting`, `program!`]),
reduceAsync({ create: () => ``, add: (a, b) => `${a} ${b}` }),
),
)
//=> Hello World! What an interesting program!

Since

v0.0.1

Call Signature

reduceAsync<Value, Acc, Finished, This>(asyncReducer, asyncIterable): Promise<Finished>

Type Parameters

Value

Acc

Finished

This

Parameters

asyncReducer

RawAsyncReducerWithFinish<Value, Acc, Finished, This> | RawReducerWithFinish<Value, Acc, Finished, This>

asyncIterable

AsyncIterable<Value, any, any>

Returns

Promise<Finished>

Defined in

reducers.d.ts:738

Call Signature

reduceAsync<Value, Acc, Finished, This>(asyncReducer): (asyncIterable) => Promise<Finished>

Type Parameters

Value

Acc

Finished

This

Parameters

asyncReducer

RawAsyncReducerWithFinish<Value, Acc, Finished, This> | RawReducerWithFinish<Value, Acc, Finished, This>

Returns

Function

Parameters

asyncIterable

AsyncIterable<Value, any, any>

Returns

Promise<Finished>

Defined in

reducers.d.ts:744

Call Signature

reduceAsync<Value, Acc, This>(asyncReducer, asyncIterable): Promise<Acc>

Type Parameters

Value

Acc

This

Parameters

asyncReducer

RawAsyncReducerWithoutFinish<Value, Acc, This> | RawReducerWithoutFinish<Value, Acc, This>

asyncIterable

AsyncIterable<Value, any, any>

Returns

Promise<Acc>

Defined in

reducers.d.ts:750

Call Signature

reduceAsync<Value, Acc, This>(asyncReducer): (asyncIterable) => Promise<Acc>

Type Parameters

Value

Acc

This

Parameters

asyncReducer

RawAsyncReducerWithoutFinish<Value, Acc, This> | RawReducerWithoutFinish<Value, Acc, This>

Returns

Function

Parameters

asyncIterable

AsyncIterable<Value, any, any>

Returns

Promise<Acc>

Defined in

reducers.d.ts:756

Call Signature

reduceAsync<Value, Finished, This>(asyncReducer, asyncIterable): AsyncOptional<Finished>

Type Parameters

Value

Finished

This

Parameters

asyncReducer

RawAsyncOptionalReducerWithFinish<Value, Finished, This> | RawOptionalReducerWithFinish<Value, Finished, This>

asyncIterable

AsyncIterable<Value, any, any>

Returns

AsyncOptional<Finished>

Defined in

reducers.d.ts:762

Call Signature

reduceAsync<Value, Finished, This>(asyncReducer): (asyncIterable) => AsyncOptional<Finished>

Type Parameters

Value

Finished

This

Parameters

asyncReducer

RawAsyncOptionalReducerWithFinish<Value, Finished, This> | RawOptionalReducerWithFinish<Value, Finished, This>

Returns

Function

Parameters

asyncIterable

AsyncIterable<Value, any, any>

Returns

AsyncOptional<Finished>

Defined in

reducers.d.ts:768

Call Signature

reduceAsync<Value, This>(asyncReducer, asyncIterable): AsyncOptional<Value>

Type Parameters

Value

This

Parameters

asyncReducer

RawAsyncOptionalReducerWithoutFinish<Value, This> | RawOptionalReducerWithoutFinish<Value, This>

asyncIterable

AsyncIterable<Value, any, any>

Returns

AsyncOptional<Value>

Defined in

reducers.d.ts:774

Call Signature

reduceAsync<Value, This>(asyncReducer): (asyncIterable) => AsyncOptional<Value>

Type Parameters

Value

This

Parameters

asyncReducer

RawAsyncOptionalReducerWithoutFinish<Value, This> | RawOptionalReducerWithoutFinish<Value, This>

Returns

Function

Parameters

asyncIterable

AsyncIterable<Value, any, any>

Returns

AsyncOptional<Value>

Defined in

reducers.d.ts:780

Call Signature

reduceAsync<Value>(asyncReducer, asyncIterable): AsyncOptional<Value>

Type Parameters

Value

Parameters

asyncReducer

AsyncFunctionReducer<Value> | FunctionReducer<Value>

asyncIterable

AsyncIterable<Value, any, any>

Returns

AsyncOptional<Value>

Defined in

reducers.d.ts:786

Call Signature

reduceAsync<Value>(asyncReducer): (asyncIterable) => AsyncOptional<Value>

Type Parameters

Value

Parameters

asyncReducer

AsyncFunctionReducer<Value> | FunctionReducer<Value>

Returns

Function

Parameters

asyncIterable

AsyncIterable<Value, any, any>

Returns

AsyncOptional<Value>

Defined in

reducers.d.ts:790