Skip to content

Instantly share code, notes, and snippets.

@cacheflow
Last active March 23, 2022 23:09
Show Gist options
  • Save cacheflow/8d0508a6177d0e5a7612113b7a37870d to your computer and use it in GitHub Desktop.
Save cacheflow/8d0508a6177d0e5a7612113b7a37870d to your computer and use it in GitHub Desktop.
const processTasks = async (tasks, wait) => {
let paused = false;
let kill = false;
const pause = () => paused = true;
const resume = () => paused = false;
const abort = () => kill = true;
let interval;
const process = () => {
interval = setInterval(() => {
let finishedProcessing = tasks.length <= 0
if (finishedProcessing || kill === true) {
clearInterval(interval);
return null;
}
if (paused === true) {
return null;
}
else {
let currentTask = tasks.shift();
currentTask.call(this)
}
}, wait)
}
return { resume, pause, abort, process }
}
const hello1 = () => console.log('hello world 1');
const hello2 = () => console.log('hello world 2');
const run = async () => {
const {resume, pause, abort, process} = await processTasks([hello1, hello2], 3000)
await process()
}
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment