Skip to main content
Deno 2 is finally here 🎉️
Learn more

Async Channels

Test

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

Example

import { Channel } from "https://deno.land/x/async_channels/mod.ts";

const sleep = (duration: number) =>
  new Promise<void>((res) => {
    setTimeout(() => res(), duration);
  });

function produce(num: number) {
  const ch = new Channel(0);
  (async () => {
    for (let i = 0; i < num; i++) {
      await sleep(500); // Do some work...
      await ch.add(i++);
    }
    ch.close();
  })();

  return ch;
}

sleep(200).then(() => console.log("boo"));

for await (let product of produce(5)) {
  console.log({ product });
}