Created
March 5, 2022 10:33
-
-
Save nullxx/f857971b0292d54fedd73905f2487014 to your computer and use it in GitHub Desktop.
in-memmory js funcion scheduler. Execute a promise function at a time. Very usable when receiving multiple events and want to process them one by one in order
This file contains 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 = []; | |
async function schedule(func) { | |
if (queue.length !== 0) return queue.push(func); // if another task is scheduled, push this task to the queue | |
queue.push(func); | |
do { | |
await queue[0](); | |
queue.shift(); | |
} while (queue.length > 0); | |
return queue.length; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment