Last active
March 23, 2022 23:09
-
-
Save cacheflow/8d0508a6177d0e5a7612113b7a37870d to your computer and use it in GitHub Desktop.
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 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