genchain is a library that provides utility functions for chaining generators.
You can install MyPackage via npm:
npm install genchain
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.
generators
: An array of generator functions.
A generator yielding the output of the last generator in the chain.
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
}
The chainAsync
function performs the same operation as chain
, but it supports asynchronous generator functions.
generators
: An array of asynchronous generator functions.
A generator yielding the output of the last generator in the chain.
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
}
})();
This project is licensed under the MIT License - see the LICENSE file for details.