batch-perform-promise
3.0版本使用新的打包配置
批量执行promise的方法,能够自定义“并行”发出的promise的个数
并行: promise发出等待时,为并行等待,由于js单线程,发出多个promise时是同步执行的,等待为并行,整体上有一种并行执行promsie的感觉
快速启动
并行发出任务
import batchPerformPromise from '../src/index';
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 '../src/index';
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);
API
async function batchPerformPromise<T>(
tasks: T[],
callback: (value: T) => Promise<any>,
concurrentSize = 2,
): Promise<TaskPayload<T>[]>