# pp-limit

Parallel Promise Limiter (pp-limit) 是一个 Promise 并发控制器，可以控制同时进行的任务数量。

## 使用

### 初始化

```ts
// 允许同时运行 5 个任务的并行控制器
const ppl = new PPLimiter(5);
```

### add - 添加任务

`add` 函数返回的 Promise 可视作输入的 Promise

```ts
const ppl = new PPLimiter(5);
ppl
  .add(() => new Promise((resolve) => resolve(1)))
  .then((result) => console.log(`${result} == 1`)); // 1 == 1
```

#### Example: 并发控制

```ts
const ppl = new PPLimiter(5);
const tasks = Array.from(
  { length: 20 },
  (_, i) => () => new Promise((resolve) => resolve(i)),
);
await Promise.all(tasks.map((t) => ppl.add(t)));
```
