Skip to content

Instantly share code, notes, and snippets.

@friendlyanon
Created January 8, 2020 21:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save friendlyanon/5063801450eb9441e47ea21906b36974 to your computer and use it in GitHub Desktop.
Save friendlyanon/5063801450eb9441e47ea21906b36974 to your computer and use it in GitHub Desktop.
/* use sleep from below, or bring your own */
// export const sleep = millis => new Promise(_ => setTimeout(_, millis));
async function impl(sentinel, func, interval, args) {
for (; !sentinel.aborted; await sleep(interval)) {
await func(...args);
}
}
export default function runAtIntervals(func, interval, ...args) {
const sentinel = { aborted: false };
const promise = impl(sentinel, func, interval, args);
return {
abort() {
sentinel.aborted = true;
},
catch: onRejected => promise.catch(onRejected);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment