Created
February 12, 2022 18:18
-
-
Save kigiri/71847e3a139798e701ca4ed98303b4d0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const allLimited = (actions, { delay = 0, concurrency = 1 }) => | |
new Promise((s, f) => { | |
let i = -1 | |
const end = actions.length | |
const result = Array(end) | |
const delayed = delay && (fn => setTimeout(fn, delay)) | |
async function next(x) { | |
if (x >= end) return x - end >= concurrency && s(result) | |
try { | |
const pause = delay && new Promise(delayed) | |
result[x] = await actions[x]() | |
pause && (await pause) | |
next(++i) | |
} catch (err) { | |
f(err) | |
} | |
} | |
while (i < concurrency) next(++i) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You have 2 options:
concurrency
limit how many actions are called in paralleldelay
add a delay in ms between each call