Skip to content

Instantly share code, notes, and snippets.

@nullxx
Created March 5, 2022 10:33
Show Gist options
  • Save nullxx/f857971b0292d54fedd73905f2487014 to your computer and use it in GitHub Desktop.
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
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