Skip to main content
ReadableBase.prototype.reduce - stream - Node documentation
method ReadableBase.prototype.reduce

Usage in Deno

import { ReadableBase } from "node:stream";
ReadableBase.prototype.reduce<T = any>(
fn: (
previous: any,
data: any,
options?: Pick<ArrayOptions, "signal">,
) => T
,
initial?: undefined,
options?: Pick<ArrayOptions, "signal">,
): Promise<T>

This method calls fn on each chunk of the stream in order, passing it the result from the calculation on the previous element. It returns a promise for the final value of the reduction.

If no initial value is supplied the first chunk of the stream is used as the initial value. If the stream is empty, the promise is rejected with a TypeError with the ERR_INVALID_ARGS code property.

The reducer function iterates the stream element-by-element which means that there is no concurrency parameter or parallelism. To perform a reduce concurrently, you can extract the async function to readable.map method.

Type Parameters

T = any

Parameters

fn: (
previous: any,
data: any,
options?: Pick<ArrayOptions, "signal">,
) => T

a reducer function to call over every chunk in the stream. Async or not.

optional
initial: undefined

the initial value to use in the reduction.

optional
options: Pick<ArrayOptions, "signal">

Return Type

Promise<T>

a promise for the final value of the reduction.

ReadableBase.prototype.reduce<T = any>(
fn: (
previous: T,
data: any,
options?: Pick<ArrayOptions, "signal">,
) => T
,
initial: T,
options?: Pick<ArrayOptions, "signal">,
): Promise<T>

Type Parameters

T = any

Parameters

fn: (
previous: T,
data: any,
options?: Pick<ArrayOptions, "signal">,
) => T
initial: T
optional
options: Pick<ArrayOptions, "signal">

Return Type

Promise<T>