Async Channels
Channels are queue-like objects (First In First Out) that their enqueue
(send) and dequeue
(get) functions are asynchronous (async
). By passing them
between asynchronous functions we can synchronize operations between said
functions.
Setup
NodeJS
Released under both npmjs & github packages:
Install:
npm
npm install @eyalsh/async_channels
yarn
yarn add @eyal-shalev/async_channels
import (ES Modules):
import { Channel } from "@eyalsh/async_channels";
require (CommonJS):
const { Channel } = require("@eyalsh/async_channels");
Deno
The library is available to import from deno.land/x/async_channels
import { Channel } from "https://deno.land/x/async_channels/mod.ts";
Browser - CDN / Download
You can import the library from any CDN that mirrors npmjs.com, such as skypack.dev or unpkg.com.
import { Channel } from "https://cdn.skypack.dev/@eyalsh/async_channels";
Or you can download compiled library from GitHub:
import { Channel } from "/path/to/async_channels.esm.js";
Note: an IIFE version also exist, if your application doesn’t support ES modules.
<script src="/path/to/async_channels.iife.js"></script>
<script>
const {Channel} = async_channels;
</script>
Examples
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 }); }