# promise-formation

> Promise.all Like function to manage promise queue

Latest version **1.0.6** (published 2022-04-28) · MIT license · 0 weekly downloads

## Install

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

## 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 | 2022-04-28 |
| First published | 2020-12-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 9.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | zobor nodejs.js@gmail.com |
| Maintainers | zobor |

## Links

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

## Recent versions

- 1.0.6 (latest) — 2022-04-28
- 1.0.5 — 2021-12-20
- 1.0.4 — 2021-10-22
- 1.0.3 — 2021-10-22
- 1.0.2 — 2020-12-30
- 1.0.1 — 2020-12-30
- 1.0.0 — 2020-12-29

## README

# promise-formation

## Document

[https://github.com/zobor/promise-formation](https://github.com/zobor/promise-formation)

promise-fomation 提供类似 Promise.all 的功能，拓展了并发控制的能力。

Promise.all 的异步执行是不能控制数量和顺序的，promise-fomation 针对这些做了优化。

场景是响应操作前、过程中、后的各种用户操作。支持控制并发允许的 Promise 数量，支持异步添加任务，支持异步停止任务。

**PromiseFormation VS Promise.all**

|                  | Promise.all | PromiseFormation |
| ---------------- | ----------- | ---------------- |
| 并行执行异步函数 | ✔           | ✔                |
| 控制任务顺序     | ✘           | ✔                |
| 限制并发数量     | ✘           | ✔                |
| 动态添加任务     | ✘           | ✔                |
| 动态停止任务     | ✘           | ✔                |
| 停止指定的任务   | ✘           | ✔                |

## 安装

```bash
$ npm install promise-formation -S
```

## 代码运行对比

### Promise.all

```js
const p1 = new Promise(resolve => {
  setTimeout(() => {
    console.log(1);
    resolve("A");
  }, 3);
});
const p2 = new Promise(resolve => {
  setTimeout(() => {
    console.log(2);
    resolve("B");
  }, 2);
});
const p3 = new Promise(resolve => {
  setTimeout(() => {
    console.log(3);
    resolve("C");
  }, 1);
});

Promise.all([p1, p2, p3]).then(rs => {
  console.log(rs);
});
// 3
// 2
// 1
// A B C
```

### Promilse.formation

```js
import promiseFormation from "promise-formation";

const p1 = () =>
  new Promise(resolve => {
    setTimeout(() => {
      console.log(1);
      resolve("A");
    }, 3);
  });
const p2 = () =>
  new Promise(resolve => {
    setTimeout(() => {
      console.log(2);
      resolve("B");
    }, 2);
  });
const p3 = () =>
  new Promise(resolve => {
    setTimeout(() => {
      console.log(3);
      resolve("C");
    }, 1);
  });

promiseFormation([p1, p2, p3]).then(rs => {
  console.log(rs);
  // 1
  // 2
  // 3
  // ["A", "B", "C"]
});
```

## 用法

```js
import promiseFormation from "promise-formation";

const getPromises = (len: number): any =>
  Array(len)
    .fill(null)
    .map((_, index) => () =>
      new Promise(async resolve => {
        await new Promise(rs => {
          setTimeout(() => {
            rs();
          }, 1000);
        });
        resolve(index + 1);
      })
    );

promiseFormation(getPromises(3)).then((res: number[]) => {
  console.log(res);
});

// 输出
[1, 2, 3];
```

### 设置并发数量

```js
const getPromises = (len: number): any =>
  Array(len)
    .fill(null)
    .map((_, index) => () =>
      new Promise(async resolve => {
        await new Promise(rs => {
          setTimeout(() => {
            rs();
          }, 1000);
        });
        resolve(index + 1);
      })
    );
promiseFormation(getPromises(9), 3).then((res: number[]) => {
  console.log(res);
});

// 输出
1;
2;
3;

4;
5;
6;

7;
8;
(9)[(1, 2, 3, 4, 5, 6, 7, 8, 9)];
```

### 异步添加任务

```js
const promiseOptions: any = {};
promiseFormation(getPromises(2), 1, promiseOptions).then((res: number[]) => {
  console.log(res);
});
promiseOptions.addOne(getPromises(1)[0]);

// 输出
[1, 2, 1];
```

### 异步停止任务

```js
const isError = v => Object.prototype.toString.call(v) === "[object Error]";
const promiseOptions: any = {};
const p = getPromises(3).map((fn: any, index: number) => {
  fn.id = `test-${index + 1}`;
  return fn;
});
promiseFormation(p, 1, promiseOptions).then((res: any[]) => {
  console.log(res.length);
  console.log(isError(res[1]));
});
promiseOptions.stopOne("test-2");

// 输出
3;
true;
```

## 参数

```js
type promiseListItem = (value: void) => Promise<any>;

promiseFormation(promiseList, max, options);
```

- promiseList: promiseListItem[] 任务列表
- max: number 最大并行任务数，默认 1
- options
  - options.addOne: (promiseListItem) => vid 添加一个任务
  - options.stop: boolean 执行过程中，修改`options.stop`为 true 之后 后面的任务全部立即返回失败(闭包内部的实参是形参的引用)
  - options.stopOne: (taskId: string) => void 指定要停止的任务

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