Last active
August 9, 2020 03:25
-
-
Save AyAyEm/9c0b35244f8c4cce17868f34505e61be to your computer and use it in GitHub Desktop.
JavaScript Callback loop class
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
class CallbackLoop { | |
constructor(asyncFunc, ...params) { | |
this.toStop = false; | |
this.asyncFunc = asyncFunc; | |
this.params = params; | |
} | |
run() { | |
const { toStop, asyncFunc, params } = this; | |
if (toStop) return; | |
asyncFunc(...params).then(() => this.run()); | |
return this; | |
} | |
pause() { | |
this.toStop = true; | |
return this; | |
} | |
resume() { | |
this.toStop = false; | |
this.run(); | |
return this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment