Created
January 2, 2020 23:40
-
-
Save dejanvasic85/5c54172face7454ae9e34414ee92a12f to your computer and use it in GitHub Desktop.
Polling in javascript
This file contains hidden or 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 poll = async (fn: any, timeout: number, interval: number) => { | |
| const endTime = Date.now() + timeout; | |
| const checkCondition = async (resolve: any, reject: any) => { | |
| const result = await fn(); | |
| if (result) { | |
| resolve(result); | |
| } else if (Date.now() < endTime) { | |
| setTimeout(checkCondition, interval, resolve, reject); | |
| } else { | |
| reject(new Error(`Timed out during polling.`)); | |
| } | |
| }; | |
| return new Promise(checkCondition); | |
| }; |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: