Skip to main content

forEach

method ReadableBase.prototype.forEach
Jump to headingReadableBase.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 Jump to heading

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

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

Return Type Jump to heading

Promise<void>

a promise for when the stream has finished.

Back to top