2.1.1 • Published 1 month ago

@jpwilliams/waitgroup v2.1.1

Weekly downloads
2,772
License
MIT
Repository
github
Last release
1 month 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.

2.1.1

1 month ago

2.1.0

2 years ago

2.0.0

3 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.2.6

4 years ago

0.2.5

4 years ago

0.2.4

4 years ago

0.2.3

4 years ago

0.2.2

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.0

4 years ago