Skip to content

Instantly share code, notes, and snippets.

@YuraKostin
Created December 13, 2021 06:51
Show Gist options
  • Save YuraKostin/de8f3f941d5f3efa3c6e5e8715b9e4bd to your computer and use it in GitHub Desktop.
Save YuraKostin/de8f3f941d5f3efa3c6e5e8715b9e4bd to your computer and use it in GitHub Desktop.
timeoutChain
const timeoutChain = (fn, { ms } = { ms: 0 }) => {
const timer = {
id: null,
unsubscribe(done = () => {}) {
clearTimeout(this.id);
this.isRunning = false;
done();
},
isRunning: true,
};
function run() {
timer.id = setTimeout(() => {
fn(() => timer.unsubscribe());
if (timer.isRunning) {
run();
}
}, ms);
}
run();
return (onDone) => () => timer.unsubscribe(onDone);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment