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.
You can install the batch-async-requests
package via npm or yarn. Follow the steps below to add it to your project.
Run the following command in your terminal:
npm install batch-async-requests
If you prefer using yarn, run:
yarn add batch-async-requests
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
});
This package includes TypeScript type definitions, so you get full type support when using it in TypeScript projects.
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 is2000
.debug?: boolean
: (Optional) Enables debug logging. If set totrue
, the function will log details about the chunk being processed.
export const batchAsyncRequests = async <T, A = void>(
data: T[],
method: (data: T[]) => Promise<A[] | void>,
{ chunkSize = 2000, debug = false }: BatchAsyncRequestsOptions
): Promise<A[]>;
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 typeA
orvoid
if no result is returned for the chunk.options: BatchAsyncRequestsOptions
: (Optional) An object containing optional settings such aschunkSize
anddebug
.
Promise<A[]>
: A promise that resolves to a concatenated array of results from each chunk. If the method returnsvoid
for a chunk, no results will be added to the output array for that chunk.
// 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]
});
- The input array,
data
, is divided into smaller chunks based on thechunkSize
option. IfchunkSize
is not specified, it defaults to2000
. - The
method
is executed asynchronously for each chunk of data. - If the
debug
option is enabled (debug: true
), the function logs the chunk number being processed to the console. - The results of each chunk (if any) are collected and concatenated into a single output array, which is returned as a promise.
- 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.