Node.js Reference ยท Streams and readline
Node.js readable.map(), filter() and other operators
Reference for the Node.js readable stream operators map, filter, forEach, take, drop, flatMap, reduce, some, every, find with examples.
Readable streams have array-like operator methods. Some return a new stream and can be chained: map(), filter(), take(), drop(), flatMap(). The others consume the stream and return a promise: forEach(), toArray(), reduce(), some(), every(), find().
Callbacks may be async. Operators that take a callback run it with limited concurrency, and all of them stop reading and destroy the source when they are done early.
const result = await Readable.from([1, 2, 3, 4]) .filter((n) => n % 2 === 0) .map((n) => n * 10) .toArray();
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Syntax
readable.map(fn[, options]): Readable readable.filter(fn[, options]): Readable readable.flatMap(fn[, options]): Readable readable.take(limit[, options]): Readable readable.drop(limit[, options]): Readable readable.forEach(fn[, options]): Promise<void> readable.reduce(fn[, initial[, options]]): Promise<any> readable.some(fn[, options]): Promise<boolean> readable.every(fn[, options]): Promise<boolean> readable.find(fn[, options]): Promise<any>
| Name | Description |
|---|---|
fn(value, { signal }) |
Callback called per chunk. May return a promise. Receives an options object with an AbortSignal. |
options.concurrency |
Maximum callbacks running at once. Default 1. Results are still emitted in input order. |
options.highWaterMark |
How many results may be buffered ahead of the consumer. |
options.signal |
An AbortSignal that aborts the operation. |
limit |
Number of chunks for take() and drop(). |
initial |
Starting accumulator for reduce(). Without it, the first chunk is the start value. |
Edge cases: reduce() of an empty stream with no initial value rejects with ERR_MISSING_ARGS. An error thrown or rejected inside a callback destroys the stream and rejects the returned promise or errors the new stream. filter() treats the return value as truthy or falsy, and an async predicate is awaited.
Usage notes
- Operators are lazy: nothing is read until a consumer pulls from the resulting stream, or a promise-returning operator runs.
- Each chained operator creates a new stream and the original is consumed by it, so do not read the source separately.
take(n)destroys the source afternchunks, which makes it safe to use with endless streams.- Chunks are whatever the stream produces:
Buffers for byte streams. These operators fit object-mode streams or iterable-based streams best. Readable.from(iterable)and the operators together cover most of what people used to install small stream utility packages for.