Repository navigation
gen
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:
import gen from 'stream-chain/gen.js';
// const {gen} = require('stream-chain/gen.js');The function takes a variable number of functions and returns an asynchronous generator function.
Functions run left to right — the value flows through them in array order, so gen(f, g, h) applies f, then g, then h (for a single plain value, gen(f, g, h)(x) produces h(g(f(x)))).
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).
Note on null/undefined: gen() is a general-purpose compositor and passes null/undefined through the pipeline like any other value, so a stage that returns nothing passes undefined to the next stage. fun(), pipe(), and chain() from stream-chain/core do the same. This differs from asStream(), asWebStream(), and chain() from stream-chain and stream-chain/web, which treat null/undefined as none (skip) at every stage because Node.js streams reserve these values for end-of-stream signaling. Use none explicitly for consistent skip behavior across all contexts.
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
}gen() doesn't flush on its own. When the input is over, call the generator function with none: every flushable stage runs its final, and what it returns goes through the stages after it.
import gen from 'stream-chain/gen.js';
import batch from 'stream-chain/utils/batch.js';
import {none} from 'stream-chain/defs.js';
const pipeline = gen(x => x * 2, batch(3));
for (const x of [1, 2, 3, 4]) {
for await (const group of pipeline(x)) console.log(group); // [2, 4, 6]
}
for await (const group of pipeline(none)) console.log(group); // [8]pipe() does both for one value: it drives the value through, then flushes.
A stop ends the data pass: the Stop exception reaches your loop. The stage that stopped and the stages before it are released right away, and the flush you call afterwards covers only the stages after it.
When the pipeline ends any other way — a stage throws, or you break out of the loop — gen() releases its stages instead of flushing them. A generator stage is closed, so its finally runs. A flushable stage gets its abort (see flushable()), unless its final has already completed.
Start here
API
Transducers
Adapters
I/O & helpers
Tuning & internals
Reference
stream-chain 2.x (legacy)