Created
April 21, 2014 18:54
-
-
Save deleteme/11152611 to your computer and use it in GitHub Desktop.
poll.coffee
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
# 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