Created
January 8, 2020 21:48
-
-
Save friendlyanon/5063801450eb9441e47ea21906b36974 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
/* 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