@freik/p-limit

中文文档
5.0.0发布于 2022/06/17MITTypeScriptESM + CJS零依赖近 30 天下载 3890

Run multiple promise-returning & async functions with limited concurrency

原始使用文档
文档目录10 节
  1. Install
  2. Usage
  3. API
  4. pLimit(concurrency)
  5. limit(fn, ...args)
  6. limit.activeCount
  7. limit.pendingCount
  8. limit.clearQueue()
  9. FAQ
  10. How is this different from the p-limit package?

p-limit

Run multiple promise-returning & async functions with limited concurrency

Do it for both CommonJS (electron, older Node) and ES Modules systems!

Install

$ npm install @freik/p-limit

Usage

Note: pLimit is now a named export, not a default export. (Default exports are annoying to deal with for dual-mode modules...)

import { pLimit } from 'p-limit';

const limit = pLimit(1);

const input = [
  limit(() => fetchSomething('foo')),
  limit(() => fetchSomething('bar')),
  limit(() => doSomething()),
];

// Only one promise is run at once
const result = await Promise.all(input);
console.log(result);

API

pLimit(concurrency)

Returns a limit function.

concurrency

Type: number
Minimum: 1
Default: Infinity

Concurrency limit.

limit(fn, ...args)

Returns the promise returned by calling fn(...args).

fn

Type: Function

Promise-returning/async function.

args

Any arguments to pass through to fn.

Support for passing arguments on to the fn is provided in order to be able to avoid creating unnecessary closures. You probably don't need this optimization unless you're pushing a lot of functions.

limit.activeCount

The number of promises that are currently running.

limit.pendingCount

The number of promises that are waiting to run (i.e. their internal fn was not called yet).

limit.clearQueue()

Discard pending promises that are waiting to run.

This might be useful if you want to teardown the queue at the end of your program's lifecycle or discard any function calls referencing an intermediary state of your app.

Note: This does not cancel promises that are already running.

FAQ

How is this different from the p-limit package?

This package is deployed as both a CommonJS and an ES Module. I work in Electron, so I need it both ways. I also live in Typescript, so I added types to the original code, and got rid of the separate type definition file.

If you like this, go to the original project author. They did most of the work.

I might fork their other suff, if I ever wind up needing any of it from the Electron main process...

版本与发布记录

@freik/p-limit 共发布 1 个版本,首次发布于 2022/06/17;过去 12 个月发布了 0 个版本。

版本发布日期距今
5.0.0latest2022/06/174 年前

关于 @freik/p-limit 的常见问题

@freik/p-limit 是什么?

Run multiple promise-returning & async functions with limited concurrency

如何安装 @freik/p-limit?

在项目目录运行 npm install @freik/p-limit,或使用 pnpm add / yarn add / bun add。

@freik/p-limit 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@freik/p-limit 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@freik/p-limit 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js ^12.20.0 || ^14.13.1 || >=16.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@freik/p-limit 可以商用吗?

@freik/p-limit 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@freik/p-limit 的下载量有多少?

根据 npm 官方下载统计,@freik/p-limit 在 2026/10/16 – 2026/11/14 这 30 天内被下载 3,890 次,日均约 130 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@freik/p-limit 还在维护吗?

最新版本 5.0.0 发布于 2022/06/17(4 年前);至今共发布 1 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/06/17。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。