Created
August 27, 2021 19:05
-
-
Save nestarz/574064d144d67f8f65d49f7adffec1dd to your computer and use it in GitHub Desktop.
queue function and execute them one by one at fixed interval
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 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