Skip to content

A Typescript utility library that let's you chain generators

License

Notifications You must be signed in to change notification settings

williammu6/genchain

Repository files navigation

genchain

genchain is a library that provides utility functions for chaining generators.

Installation

You can install MyPackage via npm:

npm install genchain

Usage

chain(generators)

The chain function chains a list of generators, executing one after the other. The output of each generator becomes the input of the next generator in the chain.

Parameters

  • generators: An array of generator functions.

Returns

A generator yielding the output of the last generator in the chain.

Example

import { chain } from 'genchain';

function* generator1() {
  yield 1;
  yield 2;
}

function* generator2(input: number) {
  yield input * 2;
}

const chainedGenerator = chain([generator1, generator2]);
for (const value of chainedGenerator) {
  console.log(value); // Output: 2, 4
}

chainAsync(generators)

The chainAsync function performs the same operation as chain, but it supports asynchronous generator functions.

Parameters

  • generators: An array of asynchronous generator functions.

Returns

A generator yielding the output of the last generator in the chain.

Example

import { chainAsync } from 'genchain';

async function* asyncGenerator1() {
  yield await Promise.resolve(1);
  yield await Promise.resolve(2);
}

async function* asyncGenerator2(input) {
  yield input * 2;
}

(async () => {
  const chainedGenerator = chainAsync([asyncGenerator1, asyncGenerator2]);
  for await (const value of chainedGenerator) {
    console.log(value); // Output: 2, 4
  }
})();

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

A Typescript utility library that let's you chain generators

Resources

License

Stars

Watchers

Forks

Packages

No packages published