Skip to content

Instantly share code, notes, and snippets.

@schahriar
Created January 14, 2018 22:23
Show Gist options
  • Save schahriar/b125dc8758d68e4f73354c77e73a2ea3 to your computer and use it in GitHub Desktop.
Save schahriar/b125dc8758d68e4f73354c77e73a2ea3 to your computer and use it in GitHub Desktop.
Conditional while loop #async-await #medium
const runFor = async (time, func, interval) => {
// Runs an interval until time is past
while (time > Date.now()) {
await timeoutPromise(interval);
// Note that you'll need to account
// for func execution time if it is
// async
func();
}
};
runFor(Date.now() + 2000, () => console.count('time'), 1000);
// Output:
// time: 1
// time: 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment