Skip to content

Instantly share code, notes, and snippets.

@nestarz
Created August 27, 2021 19:05
Show Gist options
  • Save nestarz/574064d144d67f8f65d49f7adffec1dd to your computer and use it in GitHub Desktop.
Save nestarz/574064d144d67f8f65d49f7adffec1dd to your computer and use it in GitHub Desktop.
queue function and execute them one by one at fixed interval
const queue = (fn, delay) => {
const queue = []
let curr = null
setInterval(() => {
curr = queue.shift()
}, delay)
return (...args) => {
const id = Math.random()
queue.push({ id, fn: () => fn(...args) })
return new Promise((r) => {
let interval = setInterval(() => {
if (curr?.id === id) {
r(curr.fn())
clearInterval(interval)
}
}, 10)
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment