Skip to content

Instantly share code, notes, and snippets.

@droot
Created February 26, 2012 16:31
Show Gist options
  • Save droot/1917506 to your computer and use it in GitHub Desktop.
Save droot/1917506 to your computer and use it in GitHub Desktop.
Async IF Construct using JQuery Deferred
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