Skip to content

asStream

Eugene Lazutkin edited this page Sep 17, 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');

asStream(fn[, options])

The function accepts the following arguments:

  • fn — any function, generator function, or asynchronous function.
  • options — an optional object used as an argument for the Duplex constructor.
    • See Duplex for more details.
    • If not specified readableObjectMode and writableObjectMode are set to true.

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.

Examples

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
Clone this wiki locally