📦 Creates a batched function that collects calls and processes them in groups.

Syntax

TypeScript
import { batchRun } from '@opentf/std';

batchRun<T extends any[], R>(
  batchProcessor: (argsList: T[]) => Promise<R[]>,
  options?: {
    limit?: number;
    delay?: number;
  }
): (...args: T) => Promise<R>;

Parameters

  • batchProcessor: The function that processes the collected arguments in a batch.

  • options:

    • limit: The maximum number of calls to collect before processing the batch. Default: Infinity.

    • delay: The time in milliseconds to wait before processing the batch. Default: 0.

limit must be a positive integer or Infinity, delay must be a non-negative finite number, and the batch processor must return exactly one result for each queued call.

Returns

A function that returns a Promise for the result of the specific call.

Examples

TypeScript
const batchFetch = batchRun(async (batch) => {
  // batch is an array of arguments: [[1], [2], [3]]
  const ids = batch.map(args => args[0]);
  return await fetchUsers(ids);
}, { limit: 10, delay: 50 });

const user1 = batchFetch(1);
const user2 = batchFetch(2);
Last updated on
Edit this page