Skip to content
Eugene Lazutkin edited this page Sep 18, 2024 · 5 revisions

gen() creates a pipeline of functions that is wrapped as an asynchronous generator function. All types of functions are supported in the pipeline.

In most cases you don't need to use it directly. It is used internally by the chain() function.

The example of usage:

// const {gen} = require('stream-chain');
// import {gen} from 'stream-chain';

// const gen = require('stream-chain/gen.js');
import gen from 'stream-chain/gen.js';

gen(...fns)

The function takes a variable number of functions and returns an asynchronous generator function.

Instead of a function you can use an array — all arrays will be flattened and their elements will be included verbatim. All falsy values will be simply ignored.

Functions can use any special values defined in defs (and re-exported in chain()).

If at least one function is "flushable" (see flushable functions) then the generator function will be marked as flushable.

The resulting function will be marked as having a function list (see function lists).

Examples

import gen from 'stream-chain/gen.js';

const chain = gen(
  function* (n) { for (let i = 0; i < n; ++i) yield i; },
  x => x * x
);
for await (const i of chain(3)) {
  console.log(i); // 0, 1, 4
}
Clone this wiki locally