Created
November 14, 2019 16:27
-
-
Save kruschid/9652519c5567d259543a0ac9667b7531 to your computer and use it in GitHub Desktop.
eventually
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 eventually = <T extends any>( | |
cb: () => Promise<T>, | |
timeout = 20000, | |
interval = 1000 | |
): Promise<T> => | |
timeout <= 0 | |
? Promise.reject() | |
: cb().catch(() => | |
new Promise((resolve) => { | |
setTimeout( | |
() => resolve(eventually(cb, timeout - interval, interval)), | |
interval | |
); | |
}) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment