Skip to content
Eugene Lazutkin edited this page Mar 25, 2026 · 7 revisions

fun() is an equivalent of gen(). It wraps a list of functions organized as a sequential pipeline.

fun(...fns)

The differences with gen() are:

  • It returns a function instead of an asynchronous generator function. For purely synchronous pipelines it returns a synchronous result; for asynchronous pipelines it returns a Promise.
  • Values produced by generator functions are collected in arrays and passed with many() of defs. See many values.

It exists mostly for backwards compatibility with older versions of stream-chain and for possible performance reasons.

Note on null/undefined: Like gen(), fun() is a general-purpose compositor and passes null/undefined through the pipeline like any other value. This differs from asStream() and chain(), which treat null/undefined as none (skip) because Node.js streams reserve these values for end-of-stream signaling. Use none explicitly for consistent skip behavior across all contexts.

Examples

import fun from 'stream-chain/fun.js';
import {getManyValues} from 'stream-chain/defs.js';

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

Clone this wiki locally