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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@aherve/async-pool

v1.1.0

Published

A concurrent pool for Node.js and browsers, supporting consuming result via promises, and generators.

Readme

Async Pool

A concurrent pool for Node.js and browsers, supporting consuming result via promises, and generators.

NPM Version NPM Downloads NPM License npm bundle size GitHub Actions Workflow Status

Features

  • Concurrency Control: Limit the number of promises running at the same time.
  • Memory Efficiency: Can process large numbers of tasks without having to build a large arrays of results.
  • Flexible API: Supports both async iteration and promise-based consumption.

Installation

npm install @aherve/async-pool
// with TypeScript or ES Modules
import { AsyncPool } from "@aherve/async-pool";

// or with CommonJS
const AsyncPool = require("@aherve/async-pool").AsyncPool;

Usage

Consume results as a stream

const pool = new AsyncPool<boolean>() // generic can optionally be used
  .withConcurrency(10)
  .withRetries(3); // default number of retries for each task unless specified at task level


// Processing will start as soon as the first task is added
for await (const document of largeDBCollectionScanner) {
  pool.add({
    task: () => updateDBDoc(document), // assuming this returns a Promise<boolean>
  })
}

// results can be consumed as a stream, without building a large array of results
let total = 0;
for await (const update of pool.results()) {
  if update.changed { 
    total++
  }
}

console.log("Successfully updated", total, "documents");

Safe mode

Alternatively, safe mode can be used to avoid throwing, and instead return errors in the results stream. When using this mode, the pool will process all taksks, even if some fail, and you can handle errors gracefully.

for await (const res of pool.safeResults()) {
    if (res.success) {
        console.log("Task succeeded:", res.data);
    } else {
        console.error("Task failed:", res.error);
    }
}

Headless processsing

Fire and forget tasks, with controlled concurrency and retries


const pool = new AsyncPool()
  .withConcurrency(10)
  .withRetries(3)
  .add({ task: () => doSomethingAsync() })
  .add({ task: () => doSomethingElse() })

// wait until all tasks are processed
await pool.waitForTermination();

// At this point, all promises have been resolved successfully

Promise-based consumption

Similar to Promise.all, but with controlled concurrency and builtin retries

const pool = new AsyncPool();

pool.add({ task: async () => 1 });
pool.add({ task: async () => true });
pool.add({ task: async () => "hello" });

const results = await pool.all();
console.log(results); // [1, true, "hello"], order not guaranteed (especially if retries happened)

Safe mode

all can also be used in safe mode:

const pool = new AsyncPool();
const error = new Error('nope');

pool.add({ task: async () => 1 });
pool.add({ task: async () => { throw error } });
pool.add({ task: async () => "hello" });
const results = await pool.safeAll();
/**
* [
*   { success: true, data: 1 },
*   { success: false, error },
*   { success: true, data: "hello" }
* ]
*/

Using generic typings

You can specify a generic type for the AsyncPool to enforce type safety on the results of the tasks. If you don't specify a type, it will default to unknown, allowing any type of result.

const typedPool = new AsyncPool<string>();

typedPool.add({ task: async () => "hello" }); // OK
typedPool.add({ task: async () => 1 }); // ❌ Error: Type 'Promise<number>' is not assignable to type 'Promise<string>'.

// typedPool.all() will return a Promise<string[]>, typedPool.results() is an AsyncGenerator<string>

const relaxedPool = new AsyncPool();

relaxedPool.add({ task: async () => "hello" }); // OK
relaxedPool.add({ task: async () => 1 }); // OK

// relaxedPool.all() will return a Promise<unknown[]>, relaxedPool.results() is an AsyncGenerator<unknown>

API Documentation

API docs