batch-perform-promise
5.0版本使用father打包+semaphore实现
批量执行promise的方法,能够自定义“并行”发出的promise的个数
并行: promise发出等待时,为并行等待,由于js单线程,发出多个promise时是同步执行的,等待为并行,整体上有一种并行执行promise的感觉
快速启动
并行发出任务
import { batchPerformPromise } from 'batch-perform-promise';
const payloads = ['p1', 'p2', 'p3'];
const handler = async (value: string) => Promise.resolve(value)
const tasks = await batchPerformPromise<string>(payloads, handler);
tasks[0] === {
done: true,
error: null,
payload: 'p1',
success: true,
}
并行执行时间
import { batchPerformPromise } from 'batch-perform-promise';
const payloads = ['p1', 'p2', 'p3', 'p4', 'p5'];
const handler = (payload: string) => new Promise((resolve) => {
const timer = setTimeout(() => {
clearTimeout(timer);
resolve(payload);
}, 2000);
});
await batchPerformPromise<string>(payloads, handler);
更多使用方式参考test文件