Skip to content

Instantly share code, notes, and snippets.

@griffinmichl
Last active August 7, 2022 16:20
Show Gist options
  • Select an option

  • Save griffinmichl/d5b86eab5deead37790b5308e340c787 to your computer and use it in GitHub Desktop.

Select an option

Save griffinmichl/d5b86eab5deead37790b5308e340c787 to your computer and use it in GitHub Desktop.
function queue(concurrency = 1) {
let running = 0
const taskQueue = []
const runTask = (task) => {
running++
task(() => {
running--
if (taskQueue.length > 0) {
runTask(taskQueue.shift())
}
})
}
const enqueueTask = task => taskQueue.push(task)
return {
push: task =>
running < concurrency ? runTask(task) : enqueueTask(task),
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment