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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@3-/plimit

v0.1.3

Published

Concurrency limit for async functions / 异步函数并发控制

Downloads

822

Readme

English | 中文


@3-/plimit : Concurrency Limit for Async Functions

Table of Contents

Introduction

@3-/plimit restricts the concurrency of asynchronous operations. It runs tasks under a specified concurrency threshold, queuing subsequent tasks until active slots become available.

Installation

Install using bun:

bun i @3-/plimit

Usage Demo

Import the package, initialize a limiter with the maximum concurrency, and wrap asynchronous functions.

import pLimit from "@3-/plimit";

// Initialize concurrency limit of 2
const limit = pLimit(2);

const tasks = [
  limit(() => fetch("https://api.example.com/data/1")),
  limit(() => fetch("https://api.example.com/data/2")),
  limit(() => fetch("https://api.example.com/data/3")),
];

// Resolves when all tasks complete under concurrency limit
const results = await Promise.all(tasks);

Design & Architecture

The limiter maintains an internal task queue. When a task is added:

  1. It is pushed into the queue with its promise resolution callbacks.
  2. The controller checks if the number of active tasks is below the limit.
  3. If below the limit, the next task is dequeued, and its execution begins.
  4. When a task resolves or rejects, the active count decrements, and the queue processes the next task.

Below is the execution flow of the concurrency limiter:

graph TD
    A[Task Wrapper Called] --> B{Active Count < Limit?}
    B -- Yes --> C[Increment Active Count]
    C --> D[Execute Async Function]
    D --> E[Resolve/Reject Promise]
    E --> F[Decrement Active Count]
    F --> G[Trigger Next Task]
    B -- No --> H[Queue Task]
    H --> G

Directory Structure

.
├── src/
│   └── lib.js      # Core concurrency limiting logic
└── test/
    └── main.test.js # Unit tests and usage examples

Tech Stack

  • JavaScript (ES Modules): Core implementation language.
  • Bun: Test runner and dependency management.

History & Trivia

The concept of limiting concurrency traces back to the early days of concurrent computing. In the early 1960s, Dutch computer scientist Edsger W. Dijkstra introduced the concept of the "semaphore" to solve synchronization issues in the THE multiprogramming system.

A semaphore acts as a variable that controls access to a common resource by multiple processes. The concurrency limit implementation in @3-/plimit is structurally equivalent to Dijkstra's counting semaphore, where the capacity represents the limit, and the queue coordinates task scheduling.


@3-/plimit : 异步函数并发控制

目录

功能介绍

@3-/plimit 限制异步操作并发量。此模块确保在设定的并发阈值下执行任务,并将超出限制的任务排队,直到腾出可用槽位。

安装

使用 bun 安装:

bun i @3-/plimit

使用演示

导入模块,设置最大并发数初始化限制器,包裹异步函数执行。

import pLimit from "@3-/plimit";

// 初始化并发限制为 2
const limit = pLimit(2);

const tasks = [
  limit(() => fetch("https://api.example.com/data/1")),
  limit(() => fetch("https://api.example.com/data/2")),
  limit(() => fetch("https://api.example.com/data/3")),
];

// 并发限制下执行,所有任务完成时返回结果
const results = await Promise.all(tasks);

设计思路

限制器内部维护任务队列。任务加入时:

  1. 任务及对应的 Promise 回调存入队列。
  2. 控制器检测当前活动任务数是否小于设定的并发限制。
  3. 若小于限制,从队列头部取出任务并开始执行。
  4. 任务执行完毕(无论成功或失败),递减活动任务数,并触发下一次调度。

下面是并发限制器的调用流程图:

graph TD
    A[调用限制器包装函数] --> B{活动任务数 < 限制值?}
    B -- 是 --> C[增加活动任务数]
    C --> D[执行异步函数]
    D --> E[完成或捕获异常]
    E --> F[减少活动任务数]
    F --> G[触发执行下一任务]
    B -- 否 --> H[任务入队等待]
    H --> G

目录结构

.
├── src/
│   └── lib.js      # 核心并发限制逻辑
└── test/
    └── main.test.js # 单元测试与演示代码

技术堆栈

  • JavaScript (ES Modules): 核心逻辑语言。
  • Bun: 测试运行器及依赖管理。

历史小故事

并发限制的概念源于早期并发计算。20世纪60年代初,荷兰计算机科学家艾兹赫尔·戴克斯特拉(Edsger W. Dijkstra)在设计 THE 多道程序设计系统时,提出了“信号量(Semaphore)”概念,用于解决多进程同步问题。

信号量用于控制多个进程对共享资源的访问。@3-/plimit 实现的并发限制器,在结构上相当于戴克斯特拉提出的计数信号量(Counting Semaphore)。限制值即信号量初始容量,队列负责协调任务调度。


About

This project is an open-source component of i18n.site ⋅ Internationalization Solution.

关于

本项目为 i18n.site ⋅ 国际化解决方案 的开源组件。