Skip to content

Conversation

@luckyb13
Copy link
Contributor

Pull Request: Add Promise Concurrency Limiter (pLimit.js)

Overview
This PR introduces a lightweight concurrency limiter utility similar to the widely used p-limit package. It limits the number of concurrently running async tasks and queues additional tasks until capacity is available.

Implementation Summary

  • Exports factory function pLimit(concurrency)

  • Ensures concurrency is an integer >= 1

  • Uses a FIFO queue to store pending tasks

  • Tracks active and pending counts

  • Automatically triggers queued tasks when active tasks complete

  • Includes utility accessors:

    • activeCount()
    • pendingCount()
    • clearQueue()
  • Supports both async and sync functions

Usage Example

import { pLimit } from "./pLimit.js";

const limit = pLimit(2);
const wait = ms => new Promise(r => setTimeout(r, ms));

await Promise.all(
[1,2,3,4,5].map(n => limit(async () => {
await wait(100*n);
return n;
}))
);

Validation

  • Valid concurrency validation
  • No external dependencies
  • Works in Node and browser environments
  • Handles dynamic workloads efficiently
  • FIFO ordering maintained

Notes for Reviewers

  • Small and self-contained utility
  • Well-suited for rate-limiting API calls or CPU-heavy async jobs
  • Clear code structure, documented execution flow

This addition contributes meaningful functionality and improves the utility coverage of the repository. Kindly add the label:

hacktoberfest-accepted

Thank you.

@ghostmkg ghostmkg merged commit e1fe436 into ghostmkg:main Oct 31, 2025
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants