-
-
Notifications
You must be signed in to change notification settings - Fork 12
asStream
Eugene Lazutkin edited this page Aug 20, 2024
·
3 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— no value is produced. -
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.
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