Created
May 25, 2011 09:14
-
-
Save jakcharlton/990648 to your computer and use it in GitHub Desktop.
How do I return 'data' as the result of save_it ?
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
# How do I return 'data' as the result of save_it ? | |
save_it = (edited_value, s) -> | |
$.post(url_to_save_to, {value: edited_value}, (data) -> data) |
By the way, if instead of $.post
you were calling something that synchronously runs its callback, then you could write
save_it = (edited_value, s) ->
data = null
runNow (d) -> data = d
data
to return the value provided by runNow
from save_it
. (The data = null
line is necessary to give data
scope outside of the callback.)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
data
doesn't exist until$.post
returns, which will be aftersave_it
runs, so there's no way to returndata
fromsave_it
. You need to instead do whatever you were going to do withdata
from within the$.post
callback. So for instance, instead ofyou'd write
and
I hope that helps. CoffeeScript doesn't offer any particular structures to handle asynchronicity; "it's just JavaScript," as they say.