0

For example, I want to update my_data with the session results:

    my_data = []
    url = "/some/endpoint/"

    Session.ajax
        url: url
        type: 'get'
        success: (data) ->
          my_data = data

    console.log JSON.stringify(my_data)
    return my_data

my_data is still empty, so this is definitely not working. What is the correct way of doing this?

Miu
  • 99
  • 1
  • 8
  • possible duplicate of [How to return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – Thilo Mar 18 '15 at 23:22

1 Answers1

3

That is the correct way.

my_data is empty because you log it before the request has completed (it is done asynchronously after all).

You can log (or otherwise use) it inside the success callback.

Thilo
  • 257,207
  • 101
  • 511
  • 656
  • Thank you. I have tried it inside the success callback, that works, however I would like to return the data outside of/after the session. Is that possible? edit// taking a look at the attached duplicated now. – Miu Mar 18 '15 at 23:28