Skip to main content

Variable: reduceAsync()

const reduceAsync: {<Value, Acc, Finished, This>(asyncReducer, asyncIterable): Promise<Finished>; <Value, Acc, Finished, This>(asyncReducer): (asyncIterable) => Promise<Finished>; <Value, Acc, This>(asyncReducer, asyncIterable): Promise<Acc>; <Value, Acc, This>(asyncReducer): (asyncIterable) => Promise<Acc>; <Value, Finished, This>(asyncReducer, asyncIterable): AsyncOptional<Finished>; <Value, Finished, This>(asyncReducer): (asyncIterable) => AsyncOptional<Finished>; <Value, This>(asyncReducer, asyncIterable): AsyncOptional<Value>; <Value, This>(asyncReducer): (asyncIterable) => AsyncOptional<Value>; <Value>(asyncReducer, asyncIterable): AsyncOptional<Value>; <Value>(asyncReducer): (asyncIterable) => AsyncOptional<Value>; }

Defined in: reducers.d.ts:728

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.

Call Signature​

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

Type Parameters​

Value​

Value

Acc​

Acc

Finished​

Finished

This​

This

Parameters​

asyncReducer​

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

asyncIterable​

AsyncIterable<Value>

Returns​

Promise<Finished>

Call Signature​

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

Type Parameters​

Value​

Value

Acc​

Acc

Finished​

Finished

This​

This

Parameters​

asyncReducer​

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

Returns​

(asyncIterable): Promise<Finished>

Parameters​

asyncIterable​

AsyncIterable<Value>

Returns​

Promise<Finished>

Call Signature​

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

Type Parameters​

Value​

Value

Acc​

Acc

This​

This

Parameters​

asyncReducer​

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

asyncIterable​

AsyncIterable<Value>

Returns​

Promise<Acc>

Call Signature​

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

Type Parameters​

Value​

Value

Acc​

Acc

This​

This

Parameters​

asyncReducer​

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

Returns​

(asyncIterable): Promise<Acc>

Parameters​

asyncIterable​

AsyncIterable<Value>

Returns​

Promise<Acc>

Call Signature​

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

Type Parameters​

Value​

Value

Finished​

Finished

This​

This

Parameters​

asyncReducer​

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

asyncIterable​

AsyncIterable<Value>

Returns​

AsyncOptional<Finished>

Call Signature​

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

Type Parameters​

Value​

Value

Finished​

Finished

This​

This

Parameters​

asyncReducer​

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

Returns​

(asyncIterable): AsyncOptional<Finished>

Parameters​

asyncIterable​

AsyncIterable<Value>

Returns​

AsyncOptional<Finished>

Call Signature​

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

Type Parameters​

Value​

Value

This​

This

Parameters​

asyncReducer​

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

asyncIterable​

AsyncIterable<Value>

Returns​

AsyncOptional<Value>

Call Signature​

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

Type Parameters​

Value​

Value

This​

This

Parameters​

asyncReducer​

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

Returns​

(asyncIterable): AsyncOptional<Value>

Parameters​

asyncIterable​

AsyncIterable<Value>

Returns​

AsyncOptional<Value>

Call Signature​

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

Type Parameters​

Value​

Value

Parameters​

asyncReducer​

AsyncFunctionReducer<Value> | FunctionReducer<Value>

asyncIterable​

AsyncIterable<Value>

Returns​

AsyncOptional<Value>

Call Signature​

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

Type Parameters​

Value​

Value

Parameters​

asyncReducer​

AsyncFunctionReducer<Value> | FunctionReducer<Value>

Returns​

(asyncIterable): AsyncOptional<Value>

Parameters​

asyncIterable​

AsyncIterable<Value>

Returns​

AsyncOptional<Value>

Example​

import { asAsync, mapAsync, pipe, reduceAsync, toArray } from 'lfi'

const API_URL = `https://api.dictionaryapi.dev/api/v2/entries/en`

console.log(
await pipe(
asAsync([`sloth`, `lazy`, `sleep`]),
mapAsync(async word => {
const response = await fetch(`${API_URL}/${word}`)
return (await response.json())[0].phonetic
}),
reduceAsync(toArray()),
),
)
//=> [ '/slɑθ/', '/ˈleɪzi/', '/sliːp/' ]
Playground

Since​

v0.0.1