Skip to content

Instantly share code, notes, and snippets.

@cristinecula
Created September 6, 2016 05:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cristinecula/6b4caa137ff088721f53bac023b2485f to your computer and use it in GitHub Desktop.
Save cristinecula/6b4caa137ff088721f53bac023b2485f to your computer and use it in GitHub Desktop.
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