promise-formation
v1.0.6
Published
Promise.all Like function to manage promise queue
Downloads
4
Readme
promise-formation
Document
https://github.com/zobor/promise-formation
promise-fomation 提供类似 Promise.all 的功能,拓展了并发控制的能力。
Promise.all 的异步执行是不能控制数量和顺序的,promise-fomation 针对这些做了优化。
场景是响应操作前、过程中、后的各种用户操作。支持控制并发允许的 Promise 数量,支持异步添加任务,支持异步停止任务。
PromiseFormation VS Promise.all
| | Promise.all | PromiseFormation | | ---------------- | ----------- | ---------------- | | 并行执行异步函数 | ✔ | ✔ | | 控制任务顺序 | ✘ | ✔ | | 限制并发数量 | ✘ | ✔ | | 动态添加任务 | ✘ | ✔ | | 动态停止任务 | ✘ | ✔ | | 停止指定的任务 | ✘ | ✔ |
安装
$ npm install promise-formation -S
代码运行对比
Promise.all
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
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"]
});
用法
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];
设置并发数量
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)];
异步添加任务
const promiseOptions: any = {};
promiseFormation(getPromises(2), 1, promiseOptions).then((res: number[]) => {
console.log(res);
});
promiseOptions.addOne(getPromises(1)[0]);
// 输出
[1, 2, 1];
异步停止任务
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;
参数
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 指定要停止的任务