npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

channels.ts

v1.0.2

Published

Simple and easy to use TypeScript implementation of Golang channels

Downloads

14

Readme

Installation

You can use one of the following package managers:

  • Node package manager npm:
npm install channels.ts
  • Yarn package manager yarn:
yarn add channels.ts

How to use it?

Channels

import { Channel, isClosed } from 'channels.ts';

async function main() {
    const chan = new Channel<number>();

    // Closing the channel after 10_000ms
    setTimeout(function() {
        chan.close();
    }, 10_000);

    // Producer routine
    (async function() {
        for (let i = 1; i <= 10; i++) {
            await chan.put(i);
        }
    })();

    // Consumer routine
    (async function() {
        for await (const value of chan) {
            console.log(value);
        }
    })();

    console.log("Waiting for the channel to be closed...");
    await chan.untilClosed();
    console.log("The channel has been closed.");
}

main();

Output:

Waiting for the channel to be closed...
1
2
3
4
5
6
7
8
9
10
The channel has been closed.

First we create a channel of numbers. By default the channel will have a buffer size of 1. Then we define two routines. They are simple IIFE functions that represent the go routines. They are executed immediately and we forget about them. That's why the Waiting for the channel to be closed... message appeared first. Now they can communicate with each other through our channel. The first routine writes to the channel and the second one reads from it. Finally with the chan.untilClosed() we are waiting for the channel to be closed, otherwise the program will exit immediately.

For more details see the documentation.

Select

import { Channel, Select, delay } from "channels.ts";

async function main() {
    const tick = new Channel<number>();
    const boom = new Channel<number>();
    const select = new Select();

    // Puts a value on every 100ms
    const i = setInterval(() => {
        tick.put(0);
    }, 100);

    // Puts a value after 1000ms
    const t = setTimeout(() => {
        boom.put(0);
    }, 1000)

    let b = false;
    while (!b) {
        await select
            .caseTake(tick, async () => console.log("tick."))
            .caseTake(boom, async () => {
                console.log("BOOM!");
                b = true;
            })
            .default(async () => {
                console.log("      .");
                await delay(10);
            })
            .end();
    }

    // Close all channels, intervals and timeouts
    tick.close();
    boom.close();
    clearInterval(i);
    clearTimeout(t);

    console.log("Completed")
}

main();

Wait groups

import { WaitGroup, delay } from "channels.ts";

async function worker(id: number): Promise<void> {
    console.log(`Worker ${id} starting...`);
    await delay(id * 1000);
    console.log(`Worker ${id} done`);
}

async function main() {
    const wg = new WaitGroup();

    for (let i = 1; i <= 5; i++) {
        wg.add(1);

        (async function() {
            await worker(i);
            wg.done();
        })();
    }

    console.log("Waiting...");
    await wg.wait();
    console.log("Done");
}

main();

All examples