# ajax-limiter

> Controls the maximum number of concurrent requests

Latest version **1.0.3** (published 2020-09-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install ajax-limiter
pnpm add ajax-limiter
yarn add ajax-limiter
bun add ajax-limiter
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.3 |
| Published | 2020-09-04 |
| First published | 2020-08-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | 秦旭洋 |
| Maintainers | qxy6675 |
| Keywords | limit, ajax, request |

## Links

- npm: https://www.npmjs.com/package/ajax-limiter
- Repository: https://github.com/1446445040/ajax-limiter
- Homepage: https://github.com/1446445040/ajax-limiter#readme
- Issues: https://github.com/1446445040/ajax-limiter/issues
- npm.io page: https://npm.io/package/ajax-limiter

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 1.0.3 (latest) — 2020-09-04
- 1.0.2 — 2020-08-19
- 1.0.1 — 2020-08-11
- 1.0.0 — 2020-08-11

## README

# ajax-limiter

控制并发请求的最大数量。

有别于`Promise.all`，limiter中请求发送的形式类似于滑动窗口，可以在一个请求成功或失败后，立即对结果进行处理。这可以保证请求在规定的并发限制内（默认为10个）尽快完成，且无需等到请求完成就能对已经完成的请求做处理。

适用于，**当你想要尽快完成所有请求，又想控制并发数量的时候**。如浏览器中，你有500个请求需要发送，假设因为某些原因，同时发起的请求不能超过十个，否则服务器，或浏览器就会遇到错误，就很适合这种场景。

`Node`环境同样适用。

# Installation

```
npm i --save ajax-limiter
```

# Usage

```js
const limiter = require('ajax-limiter')
const axios = require('axios') // 发送请求

const requests = [] // 返回promise的函数的数组
for (let i = 0; i < 100; i ++) {
  requests.push(() => axios.get('http://localhost:8080'))
}

limiter(requests, {
  limit: 10, // 并发数量
  exitWhenError: false, 
  success (res, index) {},
  error (e, index) {}
}).then(data => {
  console.log(data) // 按顺序排列的结果
}).catch(e => {
  console.error(e) // 只有 exitWhenError: true 时才可能会执行到这里
})
```

# Configuration

类型声明如下:

```ts
type FuncArrWithPromise = Array<() => Promise<any>>

interface Config {
  limit?: number;
  exitWhenError?: boolean;
  success?: (data: any, index: number) => any;
  error?: (e: Error, index: number) => any;
}

function limiter(arr: FuncArrWithPromise, config?: Config): Promise<any>
```

## arr （required）

数组成员为函数，每个函数都返回一个Promise对象，如果返回值不是Promise对象，默认将被resolve

## config (optional)

| 字段 | 类型 | 默认值 | 说明 |
|-----|-------|:------:|-----|
| limit | Number | 10 | 最大并发数量，也就是滑动窗口的最大值 |
| exitWhenError | Boolean | false | 请求失败时退出，类似于`Promise.all`，默认不会退出 |
| success | Function, 参数(res, index) | 无 | 单个请求**成功**时的回调函数，返回值(如果有的话)将存入最终的结果数组 |
| error | Function, 参数(err, index) | 无 | 单个请求**失败**时的回调函数，返回值(如果有的话)将存入最终的结果数组 |

# Addtional Notes

测试覆盖率 100%，具体参见`src/test`目录和`package.json`

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