Skip to main content

Works with
This package works with Bun
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Browsers
JSR Score
100%
Published
2 months ago

@jpwilliams/waitgroup

npm version Test on push Downloads

A tiny version of Golang's WaitGroup for Node.js and the browser with typings, promises, and zero dependencies.

npm install @jpwilliams/waitgroup
import http from "http";
import { WaitGroup } from "@jpwilliams/waitgroup";

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

  const urls = [
    "http://www.golang.org/",
    "http://www.google.com/",
    "http://www.somestupidname.com/",
  ];

  urls.forEach((url) => {
    // Increment the WaitGroup counter
    wg.add(1);
    // Fetch the URL
    http.get(url, () => {
      // Decrement the counter when the GET is complete
      wg.done();
    });
  });

  // Wait for all HTTP fetches to complete
  await wg.wait();
}

main();

For most applications in Node.js, using built-ins like Promise.all will work perfectly, but sometimes this can be a really nice abstraction if the promises you have to keep track of are pretty spread out.

See jpwilliams/waitgroup-deno for a Deno version.

Built and signed on
GitHub Actions
View transparency log

Add Package

deno add @jpwilliams/waitgroup

Import symbol

import * as mod from "@jpwilliams/waitgroup";

Add Package

npx jsr add @jpwilliams/waitgroup

Import symbol

import * as mod from "@jpwilliams/waitgroup";

Add Package

yarn dlx jsr add @jpwilliams/waitgroup

Import symbol

import * as mod from "@jpwilliams/waitgroup";

Add Package

pnpm dlx jsr add @jpwilliams/waitgroup

Import symbol

import * as mod from "@jpwilliams/waitgroup";

Add Package

bunx jsr add @jpwilliams/waitgroup

Import symbol

import * as mod from "@jpwilliams/waitgroup";