1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| function pLimit(concurrent) { let task = [], active = 0; return fn => new Promise(res => { task.push(async () => (active++, res(await fn()), active--, task.pop()?.())); active < concurrent && task.pop()?.(); }); }
const limit = pLimit(2); const fetchSome = size => fetch(`https://speed.cloudflare.com/__down?bytes=${size}`) const input = [ limit(() => (console.log(3), fetchSome(1024 * 1024 * 3))), limit(() => (console.log(8), fetchSome(1024 * 1024 * 8))), limit(() => (console.log(1), fetchSome(1024 * 1024 * 1))), limit(() => (console.log(0.1), fetchSome(1024 * 1024 * 0.1))), limit(() => (console.log(2), fetchSome(1024 * 1024 * 2))), limit(() => (console.log(10), fetchSome(1024 * 1024 * 10))), ];
const result = await Promise.all(input); console.log(result);
|