Skip to content

Instantly share code, notes, and snippets.

@deleteme
Created April 21, 2014 18:54
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 deleteme/11152611 to your computer and use it in GitHub Desktop.
Save deleteme/11152611 to your computer and use it in GitHub Desktop.
poll.coffee
# Periodically call the condition promise, resolves when the condition resolves
poll = (condition, interval=1000, timeout=60000)->
promise = Q.promise (resolve, reject)->
start = _.now()
test = ->
_duration = _.now() - start
if _duration > timeout
reject "Polling timeout of #{ timeout }ms exceeded."
else
Q.fcall(condition).then resolve
test()
_loop = setInterval test, interval
promise.fin -> clearTimeout _loop
promise
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment