Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

@sesamecare-oss/async-pool

Package Overview
Dependencies
Maintainers
2
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@sesamecare-oss/async-pool

A utility function for processing items from an (async) generator with a configurable concurrency limit.

latest
Source
npmnpm
Version
1.0.5
Version published
Maintainers
2
Created
Source

async-pool

@sesamecare-oss/async-pool is a TypeScript/JavaScript library that provides a utility function for processing items from an (async) generator with a configurable concurrency limit. It allows you to efficiently handle IO-bound tasks (like network requests, file operations, etc.) in parallel, while controlling how many tasks run simultaneously.

Features

  • Limit the number of concurrently running async operations.
  • Works with both async and sync generators.
  • Simple, promise-based API.
  • Written in TypeScript & fully typed.
  • Propagates errors just like native Promise.all.

Install

npm install @sesamecare-oss/async-pool

Usage

Basic Example

import { asyncPool } from '@sesamecare-oss/async-pool';

async function* generator() {
  yield 1;
  yield 2;
  yield 3;
  yield 4;
  yield 5;
}

const results: number[] = [];

await asyncPool(2, generator(), async (item) => {
  // Only 2 iterator functions run concurrently
  await doSomethingAsync(item);
  results.push(item);
});

Using with Array

const items = [1, 2, 3, 4, 5];
function* generator() {
  for (const item of items) {
    yield item;
  }
}

await asyncPool(3, generator(), async (item) => {
  // Processing up to 3 items in parallel
  await doSomethingAsync(item);
});

Collecting Results

asyncPool returns an array of all the resolved values from the iterator function, in the order items were yielded.

const items = [1, 2, 3, 4, 5];

const doubles = await asyncPool(2, items.values(), async (item) => {
  return item * 2;
});

console.log(doubles); // [2, 4, 6, 8, 10]

Error Handling

If any iterator function throws (or rejects), asyncPool will reject with that error and stop processing new items.

try {
  await asyncPool(2, [1, 2, 3][Symbol.iterator](), async (item) => {
    if (item === 2) throw new Error('fail!');
    return item;
  });
} catch (err) {
  console.error(err); // Error: fail!
}

API

async function asyncPool<T, R>(
  concurrency: number,
  iterable: Iterable<T> | AsyncIterable<T>,
  iteratorFn: (item: T, index: number) => Promise<R> | R
): Promise<R[]>;
  • concurrency: Maximum number of concurrently running tasks.
  • iterable: Any iterable or async iterable source.
  • iteratorFn: Async/sync function to execute for each item.

License

MIT

Keywords

typescript

FAQs

Package last updated on 10 Jan 2026

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts