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 🙏

© 2026 – Pkg Stats / Ryan Hefner

batch-async-requests

v1.0.5

Published

a function to batch async requests

Downloads

17

Readme

batchAsyncRequests Documentation

Overview

The batchAsyncRequests function is a utility that processes asynchronous requests in batches. This function takes an array of data and applies a given method to chunks of that data, handling each chunk asynchronously. It is useful for optimizing large sets of requests and reducing the load on APIs or other services by limiting the number of simultaneous requests.

Installation

You can install the batch-async-requests package via npm or yarn. Follow the steps below to add it to your project.

Using npm

Run the following command in your terminal:

npm install batch-async-requests

Using yarn

If you prefer using yarn, run:

yarn add batch-async-requests

Importing the Package

Once installed, you can import and use the batchAsyncRequests function in your TypeScript or JavaScript project:

import { batchAsyncRequests } from 'batch-async-requests';

// Example usage
const data = [/* your data array */];
const result = await batchAsyncRequests(data, async (chunk) => {
  // Your async operation here
});

TypeScript Support

This package includes TypeScript type definitions, so you get full type support when using it in TypeScript projects.

Type Definitions

BatchAsyncRequestsOptions

export interface BatchAsyncRequestsOptions {
  chunkSize?: number;
  debug?: boolean;
}

BatchAsyncRequestsOptions defines the optional settings for controlling the behavior of the batchAsyncRequests function.

  • chunkSize?: number: (Optional) Specifies the number of items in each chunk. The default value is 2000.
  • debug?: boolean: (Optional) Enables debug logging. If set to true, the function will log details about the chunk being processed.

Function Signature

export const batchAsyncRequests = async <T, A = void>(
  data: T[],
  method: (data: T[]) => Promise<A[] | void>,
  { chunkSize = 2000, debug = false }: BatchAsyncRequestsOptions
): Promise<A[]>;

Parameters

  • data: T[]: The array of data that will be processed in batches.
  • method: (data: T[]) => Promise<A[] | void>: The asynchronous function to be applied to each chunk of data. The method should return either a promise that resolves to an array of type A or void if no result is returned for the chunk.
  • options: BatchAsyncRequestsOptions: (Optional) An object containing optional settings such as chunkSize and debug.

Returns

  • Promise<A[]>: A promise that resolves to a concatenated array of results from each chunk. If the method returns void for a chunk, no results will be added to the output array for that chunk.

Example Usage

// Define an example method that simulates an asynchronous operation
const asyncMethod = async (chunk: number[]): Promise<number[]> => {
  return chunk.map((item) => item * 2);
};

// Call batchAsyncRequests with the data and method
const data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const options = { chunkSize: 3, debug: true };

batchAsyncRequests(data, asyncMethod, options).then((result) => {
  console.log(result); // [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
});

Behavior

  1. The input array, data, is divided into smaller chunks based on the chunkSize option. If chunkSize is not specified, it defaults to 2000.
  2. The method is executed asynchronously for each chunk of data.
  3. If the debug option is enabled (debug: true), the function logs the chunk number being processed to the console.
  4. The results of each chunk (if any) are collected and concatenated into a single output array, which is returned as a promise.

Notes

  • Chunk Processing: This method is suitable for scenarios where large datasets need to be processed, but you want to avoid overloading the system by processing the entire dataset at once. For example, batch requests to an API.
  • Error Handling: The function does not include error handling by default. You may want to wrap your method in a try/catch block to handle any errors.