forEach
fun forEach(fn: (data: Any?, options: ArrayOptions?) -> PromiseResult<Void>, options: ArrayOptions = definedExternally): Promise<Void>(source)
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 await
ed.
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.
Since
v17.5.0
Parameters
fn
a function to call on each chunk of the stream. Async or not.