close
Skip to main content
This release is versions behind 1.4.0 — the latest version of @std/async.

@std/async@1.3.0
Built and signed on GitHub Actions

Works with
This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score100%
License
MIT
Downloads26,381/wk
Publisheda month ago (1.3.0)

Utilities for asynchronous operations, like delays, debouncing, or pooling

function pooledMap
pooledMap<T, R>(
poolLimit: number,
array: Iterable<T> | AsyncIterable<T>,
iteratorFn: (data: T) => Promise<R>
): AsyncIterableIterator<R>

pooledMap transforms values from an (async) iterable into another async iterable. The transforms are done concurrently, with a max concurrency defined by the poolLimit.

If an error is thrown from iterableFn, no new transformations will begin. All currently executing transformations are allowed to finish and still yielded on success. After that, the rejections among them are gathered and thrown by the iterator in an AggregateError.

Examples

Usage

import { pooledMap } from "@std/async/pool";
import { assertEquals } from "@std/assert";

const results = pooledMap(
  2,
  [1, 2, 3],
  (i) => new Promise((r) => setTimeout(() => r(i), 1000)),
);

assertEquals(await Array.fromAsync(results), [1, 2, 3]);

Type Parameters

the input type.

the output type.

Parameters

poolLimit: number

The maximum count of items being processed concurrently. Must be a positive integer.

array: Iterable<T> | AsyncIterable<T>

The input array for mapping.

iteratorFn: (data: T) => Promise<R>

The function to call for every item of the array.

Return Type

AsyncIterableIterator<R>

The async iterator with the transformed values.

Throws

RangeError

If poolLimit is not a positive integer.

Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@std/async

Import symbol

import { pooledMap } from "@std/async";
or

Import directly with a jsr specifier

import { pooledMap } from "jsr:@std/async";