18741

coffeescript is always returning the response object

Question:

i've the following codes in coffeescript

getSection = (url) -> req = $.getJSON url return req.success (data) -> data.section

or,

getSection = (url) -> req = $.getJSON url req.success (data) -> data.section

i intended to return data.section for the function getSection. but it is always returning another object (probably the response/ajax object). how can I force to return the values in data.section from this inner function?

thanks in advance?

Answer1:

<a href="http://api.jquery.com/jQuery.getJSON/" rel="nofollow">$.getJSON</a> is an AJAX call and <strong>A</strong> stands for <strong>asynchronous</strong> so getSection will return before $.getJSON gets its response back from the server. Basically, you can't get getSection to return data.section unless you want to replace $.getJSON with <a href="http://api.jquery.com/jQuery.ajax/" rel="nofollow">$.ajax</a> and do a synchronous (i.e. non-asynchronous) call; however, synchronous calls are evil and are being deprecated so you shouldn't use them.

The usual solution is to pass a callback to getSection:

getSection = (url, callback) -> req = $.getJSON url req.success (data) -> callback(data.section)

and then you put your logic in callback rather than trying to do something with the getSection return value.

Your getSection is returning req because that's what <a href="http://api.jquery.com/jQuery.ajax/#jqXHR" rel="nofollow">req.success</a> returns and CoffeeScript functions return their final value.

Recommend

  • VBA Run-time error '1004': Selection.Interior.Pattern
  • how to create pretty urls
  • Is it safe to accept URL parameters for populating the `url_for` method?
  • Why can't UI components be accessed from a backgroundworker?
  • IE6 changes DOCTYPE to a bad one
  • How do I Dispose a HttpResponseMessage in my Web Api Method?
  • In C what exactly happens if i use () to initialize a double dimension array instead of the {}?
  • Aptana 3 remove bundle (jquery)
  • Doctrine/Symfony entity generator and generating entity from one table
  • How do I retrieve the user information of a user authenticated with Apache's mod_ldap?
  • Default parameter as generic type
  • Uncaught TypeError: $(…).select2 is not a function
  • IE11 throwing “SCRIPT1014: invalid character” where all other browsers work
  • Execute scripts AJAX returns
  • Adjust width of select element according to selected option's width
  • How can the INSERT … ON CONFLICT (id) DO UPDATE… syntax be used with a sequence ID?
  • WPF - CanExecute dosn't fire when raising Commands from a UserControl
  • Django rest serializer Breaks when data exists
  • Seeking advice on Jetty HttpClient Hang
  • Using variable in a value field in jMeter
  • Why is an OPTIONS request sent to the server?
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • DotNetZip - Calculate final zip size before calling Save(stream)
  • How to add date and time under each post in guestbook in google app engine
  • SSO with signing and signature validation doesn't work
  • Is there a mandatory requirement to switch app.yaml?
  • File upload with ng-file-upload throwing error
  • ExecuteAsync RestSharp to allow backgroundWorker CancellationPending c#
  • AngularJs get employee from factory
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • C# - Getting references of reference
  • Understanding cpu registers
  • How do I configure my settings file to work with unit tests?
  • Is it possible to post an object from jquery to bottle.py?
  • Recursive/Hierarchical Query Using Postgres
  • Running Map reduces the dimensions of the matrices
  • Binding checkboxes to object values in AngularJs
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?