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

Usage in Deno

import { ReadableBase } from "node:stream";
ReadableBase.prototype.forEach(
fn: (
data: any,
options?: Pick<ArrayOptions, "signal">,
) => void | Promise<void>
,
options?: ArrayOptions,
): Promise<void>

This method allows iterating a stream. For each chunk in the stream the fn function will be called. If the fn function returns a promise - that promise will be awaited.

This method is different from for await...of loops in that it can optionally process chunks concurrently. In addition, a forEach iteration can only be stopped by having passed a signal option and aborting the related AbortController while for await...of can be stopped with break or return. In either case the stream will be destroyed.

This method is different from listening to the 'data' event in that it uses the readable event in the underlying machinary and can limit the number of concurrent fn calls.

Parameters

fn: (
data: any,
options?: Pick<ArrayOptions, "signal">,
) => void | Promise<void>

a function to call on each chunk of the stream. Async or not.

optional
options: ArrayOptions

Return Type

Promise<void>

a promise for when the stream has finished.