Channels

Overview

We have routines running using our go function, but what if we want those routines to communicate? Let's say that our routine A needs a computation result from routine B in order to complete. That's where you need channels for the purposes of communication. All code related to channels lies in csp-coffee/channel package. Let's show an example implementation of the problem above using our library.

import { makeChannel } from 'csp-coffeep/channel';
import { go } from 'csp-coffee/go';
import { take, put } from 'csp-coffee/operators';

const ch = makeChannel();

function* adderRoutine (leftValue: number, rightValue: number) {
    yield put(ch, leftValue + rightValue)
}

function* loggerRoutine () {
    const valueToLog: number = yield take(ch);
    console.log(valueToLog) // 5
}

go(adderRoutine, 2, 3);
go(loggerRoutine);

Here we have two simple generator functions and a channel. We pass two values to adderRoutine which calculates a sum of two numbers and puts it to our channel. loggerRoutine waits for a value to appear in the channel, takes it and logs it. That's i

Last updated