# limit-promise

> run promise or async-function with limited concurrency

Latest version **1.0.6** (published 2021-02-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install limit-promise
pnpm add limit-promise
yarn add limit-promise
bun add limit-promise
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.6 |
| Published | 2021-02-28 |
| First published | 2019-10-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 4.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 27 |
| Author | jialing Lee |
| Maintainers | jialing |
| Keywords | javascript, es6, promise, limit, limit promise |

## Links

- npm: https://www.npmjs.com/package/limit-promise
- Repository: https://github.com/leejialing/limit-promise
- Homepage: https://github.com/leejialing/limit-promise#readme
- Issues: https://github.com/leejialing/limit-promise/issues
- npm.io page: https://npm.io/package/limit-promise

## Alternatives

- [promise-throttle](https://npm.io/package/promise-throttle.md) — 44.2K weekly downloads
- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads

## Recent versions

- 1.0.6 (latest) — 2021-02-28
- 1.0.4 — 2019-10-06
- 1.0.3 — 2019-10-02
- 1.0.1 — 2019-10-02
- 1.0.0 — 2019-10-02

## README

# limit promise
EN:  
Run multiple promise-returning & async functions with limited concurrency

CN:  
Promise执行数量控制，即"并发"控制（众所周知js是单线程，并不存在真正的并发，只是形象说明才这么称呼的）。

应用场景如：控制异步请求的数量。

## Install (安装)

`npm install limit-promise --save`  
or  
`yarn add limit-promise`

## Tip
EN:  
Make sure that the function you want to execute is:
* returns a Promise that matches the Promise criteria
* async function
* make sure Promise resolves or reject, otherwise it will "block" the call queue

If it doesn't meet the standard, please adapt yourself

CN:  
确保需要执行的函数是：
* 返回一个符合Promise标准的promise
* async函数
* 确保Promise会resolve或者reject，否则会"阻塞"住调用队列。

如果不符合标准请自行适配一下。

## Usage（使用）

```js

// request.js
// a request module, contain 'get' and 'post' function

// limitRequest.js
const LimitPromise = require('limit-promise')
const request = require('./request')
// request maximum limit 并发请求上限
const MAX = 5
// core controller 核心控制器
const limitP = new LimitPromise(MAX)

// 通过控制器包装get和post方法，实际上是将请求函数递交给控制器处理
// Wrapping the get and post methods with the controller,
// The request function is actually handed over to the controller for processing
function get (url, params) {
  return limitP.call(request.get, url, params)
}

function post (url, params) {
  return limitP.call(request.post, url, params)
}

module.exports = {get, post}

// example.js

// import new request module
const request = require('./limitRequest')
// mock apis
const apis = [
  {url: 'www.baidu.com'},
  {url: 'www.taobao.com'},
  {url: 'www.qq.com'},
  {url: 'www.jd.com'},
  {url: 'www.meituan.com'},
  {url: 'www.toutiao.com'},
  {url: 'www.weibo.com'},
  {url: 'www.zhihu.com'},
  {url: 'www.alipay.com'}
]

// start multiple network request
// Because the maximum number of requests is limited to 5, the first five requests will start immediately, and the sixth will start in the ready queue for execution
// Once a request is completed and returned, a task is removed from the queue for execution
// 简单粗暴的使用for循环启动数个请求
// 由于限制了最大请求数量是5，因此前五个请求将立即启动，而第6个开始将进入就绪队列等待执行
// 一旦某个请求结束并返回，将从队列中取下一个任务执行
for (let i = 0; i < apis.length; i++) {
  request.get(apis[i].url, {id: i})
    .then(res => console.log(`request resolve : ${res}`))
}

```

## License
*MIT*

---
_Source: https://npm.io/package/limit-promise · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
