@eyalsh/async_channels

Inspired by Go & Clojure Channels, async_channels provides channels as an asynchronous communication method between asynchronous functions.

Usage no npm install needed!

<script type="module">
  import eyalshAsyncChannels from 'https://cdn.skypack.dev/@eyalsh/async_channels';
</script>

README

Async Channels

Latest Version Test & Release codecov nodejs minimum version License: GPL v3

Inspired by Go & Clojure Channels, async_channels provides channels as an asynchronous communication method between asynchronous functions.

Installation

Vanilla JS (CDN)

Import the module from one of the CDNs that mirror npmjs.com:

import { Channel } from "https://cdn.skypack.dev/@eyalsh/async_channels";
const ch = new Channel();
// ...

Vanilla JS (Download)

A compiled version exists for every major dependency management system in the releases section.
Download one of them and import it

import { Channel } from "/path/to/async_channels.esm.js";
const ch = new Channel();
// ...

NPM (ESM)

Released under both npmjs & github packages:

import { Channel } from "@eyalsh/async_channels"; // or "@eyal-shalev/async_channels" for github packages.
const ch = new Channel();
// ...

NPM (CommonJS)

Released under both npmjs & github packages:

const { Channel } = require("@eyalsh/async_channels"); // or "@eyal-shalev/async_channels" for github packages.
const ch = new Channel();
// ...

Deno

import { Channel } from "https://deno.land/x/async_channels/mod.ts";
const ch = new Channel<unknown>();

Examples

  • middleware (ping-pong)

  • pipeline (even or odd)

  • Message Queue

  • static site (todos app)

  • import { Channel, time } from "https://deno.land/x/async_channels/mod.ts";
    
    function produce(num: number) {
      return Channel.from((async function* () {
        for (let i = 0; i < num; i++) {
          await time.timeout(100).get(); // Do some work...
          yield i;
        }
      })());
    }
    
    time.timeout(300).get().then(() => console.log("boo"));
    
    for await (const product of produce(4)) {
      console.log({ product });
    }