-1

It would be very useful to me if you could help me fix this function:

textParseQuery = (txtSnippet) ->    
    queryUrl = "http://localhost:8083/txtParse/#{txtSnippet}"
    console.log queryUrl
    callback = (response) => 
        parsed = $.parseJSON response
        companies = parsed.map (obj) -> new Company(obj.name, obj.addr)
        companies
    res = $.get queryUrl, {}, callback
    console.log res

I would like to fetch the results from the callback so that the textParseQuery function could return a value.

NoIdeaHowToFixThis
  • 4,484
  • 2
  • 34
  • 69
  • possible duplicate of [Variable doesn't get returned from AJAX function](http://stackoverflow.com/questions/12475269/variable-doesnt-get-returned-from-ajax-function) – Denys Séguret Apr 14 '14 at 11:35
  • I'd like to have an answer with idiomatic coffeescript code (use of the fat arrow ´=>´?) – NoIdeaHowToFixThis Apr 14 '14 at 11:39
  • You mean you don't know how to convert JS to and from CS ? Use JS then. – Denys Séguret Apr 14 '14 at 11:40
  • translating JS to coffeescript might mean not using peculiar features of latter. As I am trying to learn coffeescript, it'd be useful seeing how things are done differently in this language compared to javascript – NoIdeaHowToFixThis Apr 14 '14 at 11:46
  • There's no difference regarding callbacks or promises. CS only adds a cosmetic layer here, you have to understand how it works behind. – Denys Séguret Apr 14 '14 at 11:47

2 Answers2

1

The point of a callback is it's asynchronous, your response comes in the callback, so you need to handle the rest of the execution from the callback (e.g., the console.log res is going to execute before your callback is called, since it's part of the same synchronous execution of your ajax call).

textParseQuery = (txtSnippet) ->    
    queryUrl = "http://localhost:8083/txtParse/#{txtSnippet}"
    callback = (response) -> 
        parsed = $.parseJSON response
        companies = parsed.map (obj) -> new Company(obj.name, obj.addr)

        # proceed from here
        console.log companies
    $.get queryUrl, {}, callback

Additional note: the fat arrow is unnecessary here, it's used to rebind what this refers to, but you aren't referencing this at all in your callback. If you're learning coffee, most editors will have plugin/modules to quickly compile coffee to JS, so use that to see what a given coffee syntax compiles to in JS (e.g., take a look at the diff between using -> and => when you compile your coffee)

dule
  • 17,798
  • 4
  • 39
  • 38
0

I have discovered IcedCoffeeScript helps streamline the asynchronous control flow with await and defer. This is what I have tried to achieve. The code structure is how I pictured it

# Search for 'keyword' on twitter, then callback 'cb'
# with the results found.
search = (keyword, cb) ->
  host = "http://search.twitter.com/"
  url = "#{host}/search.json?q=#{keyword}&callback=?"
  await $.getJSON url, defer json
  cb json.results
NoIdeaHowToFixThis
  • 4,484
  • 2
  • 34
  • 69