For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /reference/promise/limitAsync.md.

limitAsync es-toolkit

Creates a new function that limits the maximum number of concurrent executions of an async function.

const limitedFunc = limitAsync(asyncFunc, concurrency);

Reference

limitAsync(callback, concurrency)

Use limitAsync when you want to limit the number of concurrent executions of an async function that gets called multiple times. Only the specified number of executions run concurrently, and additional calls wait until running operations complete.

import { limitAsync } from 'es-toolkit/promise';

// Limit to at most 3 concurrent requests.
const limitedFetch = limitAsync(async url => {
  return await fetch(url);
}, 3);

const urls = ['url1', 'url2', 'url3', 'url4', 'url5'];
await Promise.all(urls.map(url => limitedFetch(url)));
// The first 3 execute first, then the rest execute as they complete.

It's useful for controlling load on resource-intensive operations like external API calls or database queries. It's especially effective when working with rate-limited APIs or managing server load.

import { limitAsync } from 'es-toolkit/promise';

// Process heavy computations at most 2 at a time.
const processItem = async item => {
  return await heavyComputation(item);
};

const limitedProcess = limitAsync(processItem, 2);
const items = [1, 2, 3, 4, 5];
await Promise.all(items.map(item => limitedProcess(item)));
// Only 2 items are processed concurrently at most.

Parameters

callbackF extends (...args: any[]) => Promise<any>required

The async function to limit concurrent executions for.

concurrencynumberrequired

Maximum number of concurrent operations.

Returns

A new function with concurrency limiting applied. It has the same interface as the original function.

F
Source: es-toolkit

Re-exported verbatim from es-toolkit. Implementation, edge cases, and performance behavior are owned upstream. This page mirrors the documentation at the pinned version; the linked source is authoritative.