JSPM

promises-limiter

1.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 8
  • Score
    100M100P100Q24746F
  • License MIT

PromisesLimiter is a class for limiting concurrent asynchronous requests, allowing them to be executed in batches with control over progress, handling of successful and failed requests.

Package Exports

  • promises-limiter
  • promises-limiter/dist/index.js
  • promises-limiter/dist/index.mjs

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (promises-limiter) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Promises Limiter

PromisesLimiter is a class for limiting concurrent asynchronous requests, allowing them to be executed in batches with control over progress, handling of successful and failed requests.

Description

This class allows you to execute asynchronous functions with a limit on the number of concurrent executions. You can configure delays between batches, success and error handlers, and receive progress updates during execution.

Installation

npm install promises-limiter

Usage

Import

import PromisesLimiter from 'promises-limiter';

Constructor

new PromisesLimiter<T, E>(
  requests: AsyncFunction<T>[],
  config?: Partial<LimitConfig<T, E>>
);
  • requests: An array of asynchronous functions to be executed.
  • config: A configuration object with the following parameters:
    • maxConcurrent: Maximum number of concurrent requests (default is 10).
    • delayBetweenBatches: Delay between batch executions in milliseconds (default is 0).
    • progressiveDelayStep: Delay step added after each batch (default is 0).
    • maxProgressiveDelay: Maximum delay for batches (default is 0).
    • onSuccess: Function to handle successful results.
    • onError: Function to handle errors.
    • onProgress: Function called to track progress.
    • onComplete: Function called when all requests are completed.

Methods

  • cancel(): Cancels all requests that have not yet completed.

  • run(): Promise<{ success: T[]; failed: E[] }>: Starts executing requests and returns an object containing arrays of successful and failed results.

Example

const requests: AsyncFunction<number>[] = [
  async (signal) => { /* ... */ },
  async (signal) => { /* ... */ },
  // Other requests
];

const limiter = new PromisesLimiter<number, Error>(requests, {
  maxConcurrent: 5,
  delayBetweenBatches: 1000,
  onSuccess: (result) => console.log('Success:', result),
  onError: (error) => console.error('Error:', error),
  onProgress: (progress) => console.log('Progress:', progress),
  onComplete: (results) => console.log('Completed:', results),
});

limiter.run().then(({ success, failed }) => {
  console.log('Completed successfully:', success);
  console.log('Failed requests:', failed);
});