Skip to content

Instantly share code, notes, and snippets.

@AyAyEm
Last active August 9, 2020 03:25
Show Gist options
  • Save AyAyEm/9c0b35244f8c4cce17868f34505e61be to your computer and use it in GitHub Desktop.
Save AyAyEm/9c0b35244f8c4cce17868f34505e61be to your computer and use it in GitHub Desktop.
JavaScript Callback loop class
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