Skip to content

A tiny version of Golang's WaitGroup with typings, promises, and zero dependencies.

License

Notifications You must be signed in to change notification settings

jpwilliams/waitgroup

Repository files navigation

@jpwilliams/waitgroup

npm version JSR Test on push Downloads

A tiny version of Golang's WaitGroup.

  • Tiny (<1kB)
  • Works with browsers Deno Node.js Cloudflare Workers Bun
  • Typed
  • Zero dependencies 🎉

There are too many package managers 🥲

# From npm
npm install @jpwilliams/waitgroup
yarn add @jpwilliams/waitgroup
pnpm add @jpwilliams/waitgroup
# From JSR
npx jsr add @jpwilliams/waitgroup
bunx jsr add @jpwilliams/waitgroup
deno add @jpwilliams/waitgroup
yarn dlx jsr add @jpwilliams/waitgroup
pnpm dlx jsr add @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 strictly Deno version, or @jpwilliams/waitgroup on JSR.

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy