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 🙏

© 2024 – Pkg Stats / Ryan Hefner

promise-queue-limiter

v1.0.2

Published

A simple and efficient module for managing concurrent promises with a queue and concurrency limiter.

Downloads

211

Readme

🐻 PromiseQueueLimiter

PromiseQueueLimiter is a simple class for controlling the number of concurrently running asynchronous operations. It helps limit the number of async functions executed at the same time and maintains a queue for tasks that cannot be executed immediately.

Installation

You can install this module via npm:

npm install promise-queue-limiter

Description

The PromiseQueueLimiter class allows you to effectively manage asynchronous functions by limiting the number of concurrent operations. This is useful when working with resources that have limitations on the number of simultaneous requests ( e.g., APIs with rate limiting, databases, or file systems).

Key Features:

  • Limits the number of concurrent asynchronous operations.
  • Maintains a queue for tasks waiting to be processed.
  • Provides the ability to clear the queue.

Usage

Import and Initialization

import { PromiseQueueLimiter } from 'promise-queue-limiter';

const queue = new PromiseQueueLimiter({ concurrent: 3 }); // Up to 3 concurrent requests

Example Usage

const fetchData = (url: string) => {
  return fetch(url).then(response => response.json());
};

// Add an asynchronous function to the queue
queue.add(fetchData, 'https://api.example.com/data1').then(data => {
  console.log('Received data:', data);
});

queue.add(fetchData, 'https://api.example.com/data2').then(data => {
  console.log('Received data:', data);
});

queue.add(fetchData, 'https://api.example.com/data3').then(data => {
  console.log('Received data:', data);
});

queue.add(fetchData, 'https://api.example.com/data4').then(data => {
  console.log('Received data:', data);
});

In this example, PromiseQueueLimiter limits the number of concurrent requests to 3. Other requests will wait until the previous ones are completed before starting new ones.

Configuration

The constructor accepts an object with the following parameter:

  • concurrent (default: 1) — the maximum number of tasks to execute concurrently. For instance, if you set concurrent: 5, no more than five tasks will run at the same time.
const queue = new PromiseQueueLimiter({ concurrent: 5 });

Methods

  • add(method, ...args): Adds an asynchronous function to the queue.

    • method: The asynchronous function to be executed.
    • args: The arguments to be passed to the function.
  • clear(): Clears the queue and removes all active promises.

    queue.clear();
  • size: Returns the number of tasks in the queue.

    console.log(queue.size); // 2
  • isEmpty: Returns true if the queue is empty.

    console.log(queue.isEmpty); // false

Many requests at one time

Bad way

⚠️ Warning: For most apis this way create many errors

In this example you send 1000 requests at one time to some server. Most apis returns error 429 "Too Many Requests" for so many requests.

import api from './services'

const textList = [
  'Some text 1',
  'Some text 2',
  // ...
] // 1000 elements

// create 1000 requests at one time
const promises = textList.map(text => api.checkText(text))

Promise.allSettled(promises).then(() => {
  console.log('completed all checking requests')
})

Good way

✅ Works for most apis

concurrent: number

import PromiseQueueLimiter from 'promise-queue-limiter'
import api from './services'

const queue = new PromiseQueueLimiter({
  concurrent: 5
})

const textList = [
  'Some text 1',
  'Some text 2',
  // ...
] // 1000 elements

// create only 5 requests at one time
const promises = textList.map(text => queue.add(api.checkText, text))

Promise.allSettled(promises).then(() => {
  console.log('completed all checking requests')
})

Conclusion

PromiseQueueLimiter is a convenient tool for managing asynchronous operations with a limited number of concurrent tasks. It’s perfect for scenarios where it’s important to control parallelism to avoid overloading a server or an external API.**

Repository

You can find the source code and contribute to the project on GitHub.