Creation

fromArray<Arr extends any[]>(arr: Arr): Channel<Flatten>

fromArray will take an array of values and create a channel which will contain all of these values. The result channel will be closed when all values are taken.

import { fromArray, iterate } from 'csp-coffee/operators'
import { go } from 'csp-coffee/go';

const ch = fromArray([1, 2, 3]);

function* testGenerator () {
    yield iterate((value) => {
        console.log(value);
    }, ch);
}

go(testGenerator)
// 1
// 2
// 3

fromPromise<PromiseType extends Promise>(promise: PromiseType, { bufferType, capacity }?: ChannelConfiguration): Channel<PromiseResponseType>

fromPromise will take a promise and produce a channel which will contain a value resolved from promise. Result channel will be closed once that value is taken. You can additionally pass a buffer type and capacity of a result channel. By default result channel will have an Unblocking buffer with unlimited capacity.

import { fromPromise, iterate } from 'csp-coffee/operators'
import { go } from 'csp-coffee/go';

const ch = fromPromise(Promise.resolve(1));

function* testGenerator () {
    yield iterate((value) => {
        console.log(value);
    }, ch);
}

go(testGenerator)
// 1

Last updated