An open API service indexing awesome lists of open source software.

https://github.com/bakerface/csp

Communicating Sequential Processes in TypeScript
https://github.com/bakerface/csp

Last synced: 8 months ago
JSON representation

Communicating Sequential Processes in TypeScript

Awesome Lists containing this project

README

          

# CSP
**Communicating Sequential Processes in TypeScript**

## Example

``` typescript
import * as csp from "@bakerface/csp";

const sleep = (ms: number) =>
new Promise((resolve) => setTimeout(resolve, ms));

async function producer(
ch: csp.OutputChannel,
id: string,
idle: number
) {
for (;;) {
await sleep(idle);
await csp.put(ch, id);
}
}

async function consumer(ch: csp.InputChannel) {
for (;;) {
const id = await csp.take(ch);
console.log(id);
}
}

async function main() {
const ch = csp.chan();

await Promise.all([
producer(ch, "250ms", 250),
producer(ch, "1s", 1000),
consumer(ch),
]);
}

main().catch((err) => {
console.error(err);
process.exit(1);
});
```