-
-
Notifications
You must be signed in to change notification settings - Fork 14
asStream
Eugene Lazutkin edited this page Jun 6, 2026
·
7 revisions
asStream() creates a Duplex stream from any function. It supports regular functions,
asynchronous functions, generator functions, and asynchronous generator functions.
The result can be used as a regular stream.
In most cases you don't need to use it directly. It is used internally by chain().
The example of usage:
import asStream from 'stream-chain/asStream.js';
// const {asStream} = require('stream-chain/asStream.js');The function accepts the following arguments:
-
fn— any function, generator function, or asynchronous function. -
options— an optional object used as an argument for theDuplexconstructor.- See Duplex for more details.
- If not specified
readableObjectModeandwritableObjectModeare set totrue.
The function returns a Duplex stream, which wraps the fn.
fn can use any special values defined in defs (and re-exported in chain()):
-
none,null,undefined— no value is produced. Node.js streams reservenull/undefinedfor end-of-stream signaling, so they are treated asnone. -
stop,Stop— terminates the stream. -
many()— multiple (or none) values are produced. -
finalValue()— its payload is treated as a regular value.- Native streams do not support this feature.
Note: This differs from gen() and fun(), which are general-purpose compositors and pass null/undefined through the pipeline like any other value.
import asStream from 'stream-chain/asStream.js';
const stream = asStream(x => x * x);
dataSource.pipe(stream);
// if dataSource produces: 1, 2, 3
// then the result will be: 1, 4, 9-
asWebStream()— the Web Streams counterpart. Same dual-role API (pass a Web Streams object → returned as-is; pass a function → returns a duplex pair). - highWaterMark — backpressure tuning.
Start here
API
Transducers
Adapters
I/O & helpers
Tuning & internals
Reference
stream-chain 2.x (legacy)