Skip to content

Instantly share code, notes, and snippets.

@tkesgar
Last active April 29, 2021 01:39
Show Gist options
  • Save tkesgar/75633908dd5062dbce7b66d7ba7a3aa5 to your computer and use it in GitHub Desktop.
Save tkesgar/75633908dd5062dbce7b66d7ba7a3aa5 to your computer and use it in GitHub Desktop.
// https://twitter.com/antonybudianto/status/1387211508296261633
async function serialCon(fns, limit) {
const queue = [...fns]
const workers = []
for (let i = 0; i < limit; i += 1) {
workers.push((async () => {
while (queue.length > 0) {
const fn = queue.shift()
await fn()
}
})())
}
await Promise.all(workers)
}
const wait = ms => new Promise(resolve => setTimeout(resolve, ms))
serialCon(
[5000, 100, 200, 300, 400, 500].map(ms => async () => {
await wait(ms)
console.log(ms)
}),
1
).catch(console.error)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment