Created
January 14, 2018 22:23
-
-
Save schahriar/b125dc8758d68e4f73354c77e73a2ea3 to your computer and use it in GitHub Desktop.
Conditional while loop #async-await #medium
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
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