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

async-pool-js

v1.0.0

Published

Manage concurrent workers with optional rate-limiting

Readme

Pool.js

Pool.js makes it easy to execute async tasks in parallel, with optional concurrency and/or rate limits for rate-limited APIs.

It's a lightweight, one-file solution with zero dependencies and works in both Node.js and the browser.

For more on why I wrote this, see the blog post.

Installation

npm install pool-js

Usage

Pool.js works best with async iterables. Here's an example of using it with the Stripe API (uses my helper function for getting async iterators from Stripe Lists).

import { iterateList } from "./stripeiterators";

// This creates an async iterable that will iterate over all
// balance transactions in your Stripe account.
const balanceTransactions = iterateList((starting_after, limit) =>
  stripe.balanceTransactions.list({
    starting_after,
    limit,
  }),
);

// Here's the user-defined async task that will be called
// for each balance transaction.
async function processTransaction(transaction) {
  if (transaction.type === "payment") {
    const chargeId = transaction.source;
    const payment = await stripe.charges.retrieve(chargeId);
    console.log(payment.amount);
    // Do stuff
  }
}

// Kicks off the pool with 100 concurrent tasks and a
// rate limit of 100 tasks per second.
await iterateWithPool(
  balanceTransactions,
  { rate: 100, concurrency: 100 },
  processTransaction,
);

You can give Pool.js a maximum number of concurrent tasks, and/or a strict rate limit. The rate limit is implemented using a token-bucket-like algorithm which matches what Stripe (any many other API rate limiters) uses internally.

In your task method, you can return false if you want to cancel execution entirely:

async function processTransaction(transaction) {
  if (transaction.type !== "payment") {
    console.log("Unexpected transaction type!");
    return false; // Cancels execution without throwing an error!
  }
}

If any of your tasks throws an error, it will be captured and bubbled up to the caller of iterateWithPool() only after waiting for any pending tasks to complete.

AsyncPool Class

If you can't use async iterators, you can use the AsyncPool class directly for complete control:

const pool = new AsyncPool(options);

// Example loop that adds tasks to the pool.
for (const item of items) {
  // Check if the pool has been canceled by a task.
  if (pool.stopped) break;

  // Add a task to the pool and wait until it's safe to add another.
  // This method may also throw an Error if one if the tasks threw one.
  await pool.add(async () => {
    // Call some user function to do some work.
    const result = await doSomeWork(item);

    // If you returned false, request that the pool be canceled.
    if (result === false) pool.cancel();
  });
}

// We must wait for the pool to finish no matter what.
// This method may also throw an Error if one if the tasks threw one.
await pool.finish();