0

I am trying to solve the problem of assigning values returned from getJSON() functions to variables, after the getJSON() has finished.

I think the following resolves the timing issues, but I have run into another problem which is extricating the value returned from the done() function and assigning it to a variable.

var fn1 = $.getJSON("/path", function(){
});

var h = fn1.done(function (results) {
console.log(results.a_key);  // this logs the desired value
console.log(jQuery.type(results.a_key)); // this logs 'string'
return results.a_key;
});

alert(h); // alerts [object Object]

How do I access the returned value of a done() function that is assigned to a variable?

This is not a timing issue question, it is about how to access the returned value.

If the above is the wrong approach, could somebody demonstrate how they would resolve the issue and assign the result to a variable outside of the function?

user1063287
  • 10,265
  • 25
  • 122
  • 218
  • you can't return a value like that... since it is processed asynchronously all code that depends on the result of the ajax request should be within the handler – Arun P Johny Jan 22 '14 at 08:36
  • possible duplicate of [How to return the response from an AJAX call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – Arun P Johny Jan 22 '14 at 08:37
  • @ArunPJohny I previously asked a question that was marked as a duplicate of that question here (http://stackoverflow.com/questions/21238939/how-to-assign-the-return-value-of-a-function-containing-getjson-to-a-variable), I believe the above solves the timing issues, but I still don't know how to assign the returned value to a variable. – user1063287 Jan 22 '14 at 08:40
  • @user1063287 the answer is you cann't assign it to a variable.. except to a global/closure variable which will get updated asynchronously – Arun P Johny Jan 22 '14 at 08:42
  • @ArunPJohny - could you demonstrate an alternative way to resolve the issue and assign the returned value to a variable? I'm thinking there must be a way to access the returned value of a function from outside a function? – user1063287 Jan 22 '14 at 08:55

2 Answers2

0

done returns a jQuery Deferred object - it is not, ever, a return value.

Since deferred.done() returns the deferred object, other methods of the deferred object can be chained to this one, including additional .done() methods.

(The displayed value is "[object Object]" as this is the [[ToString]] of a deferred object.)

Once you start using promises (or other async callbacks), you are stuck with the approach - but that's fine, keep on!

// A done() call always returns the same deferred object (fn1 in this case)
// so don't `return` from it.
fn1.done(function (results) {
    console.log(results.a_key);  // this logs the desired value
    console.log(jQuery.type(results.a_key)); // this logs 'string'

    // Do stuff with results here INSIDE the callback
    // (Could also attach an additional `done` or use `then` as appropriate)
    alert(results.a_key)    
});

// Code down here is [likely] run before the done callback runs
// as the callback is "asynchronous".

(Promises/A and jQuery Deferred objects really become fun when use with then and more complex asynchronous execution flows.)

user2864740
  • 60,010
  • 15
  • 145
  • 220
0

Solution

This worked for me as a solution in my scenario.

Instead of this paradigm:

  • function_1 returns a result asynchronously
  • a = result of function_1
  • use a in function_2 here

I switched to:

  • a = function_1
  • when function_1 is done
  • b = result of function_1
  • use b in function_2 here

So the function_1.done() function became a container for function_2.

Example:

// here is the function that you are waiting on results from
var myGetJsonFunction = $.getJSON("/path", function(){
});
// the following is activated once the above function has run
myGetJsonFunction.done(function (results) {
var variableFromResults = results.a_key;
// use variableFromResults in a function here
});

// call the 'container' function here
$(document).on("click",".form_submit", function (e) {
e.preventDefault();
myGetJsonFunction();
});
user1063287
  • 10,265
  • 25
  • 122
  • 218