Created
September 6, 2016 05:38
-
-
Save cristinecula/6b4caa137ff088721f53bac023b2485f to your computer and use it in GitHub Desktop.
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
tryTimes = (f, times, interval) -> | |
new Promise (resolve, reject) -> | |
tries = 0 | |
doIt = -> | |
++tries | |
try | |
result = f() | |
if result | |
clear() | |
return resolve(result) | |
if tries is times | |
clear() | |
reject(new Error('Failed')) | |
interval = setInterval doIt, interval | |
clear = -> clearInterval(interval) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment