New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

promises-limiter

Package Overview
Dependencies
Maintainers
0
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

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.

latest
Source
npmnpm
Version
1.0.0
Version published
Maintainers
0
Created
Source

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);
});

Keywords

promises

FAQs

Package last updated on 03 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts