Created
February 26, 2012 16:31
-
-
Save droot/1917506 to your computer and use it in GitHub Desktop.
Async IF Construct using JQuery Deferred
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
async_if = (fn, args, timeout)-> | |
dfd = new jQuery.Deferred() | |
curr_probe = null | |
probe = ()-> | |
if fn(args) | |
dfd.resolve(args) | |
curr_probe = null | |
else | |
curr_probe = setTimeout probe, 5 | |
if timeout | |
setTimeout(()-> | |
curr_probe and clearTimeout curr_probe | |
dfd.reject(args) | |
,timeout) | |
probe() | |
return dfd.promise() | |
root = exports ? this | |
root.async_if = async_if |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment