0

I am already prepared for downvoters sitting in front of the screen waiting for the perfect moment. I also know this will be marked as duplicate by someone but if there are 1000s of duplicates for this question, then there must be some more things to be clarified for the subject.. So my question is for those who can emphatise that I have tried most of the solutions for the duplicate questions and still did not achieve a desired result..

var myobj = {};

var objpromise = $.getJSON('path_to.json');

objpromise.done(function(result) {
   myobj = result; 
   console.log(myobj.someproperty) // {"key": "value"} it is the desired result
});

console.log(myobj) // undefined.

now, many examples and suggestions say I should do my work within the .done callback. however, the result is an object where I will be using it throughout many places down below in my code.

In this case "yet again" how can I store the result in a global variable and ensure it is set before using it down below the code?

Subliminal Hash
  • 13,614
  • 20
  • 73
  • 104
  • 1
    Promises in javascript are running asynchronously, so your console.log(myobj) will probably be called before your promise is done and the variable is not set. That's why it is undefined. – Trickzter Mar 30 '16 at 13:29
  • 1
    Again, just like your last question this is a duplicate. Put all code reliant on the result of the AJAX request ***in the callback***. – Rory McCrossan Mar 30 '16 at 13:29
  • "how can I store the result in a global variable and ensure it is set before using it down below the code" - ONLY way is to put all your down below the code inside the the done callback – shershen Mar 30 '16 at 13:29
  • [This question](http://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) is also very relevant. – James Thorpe Mar 30 '16 at 13:30
  • *"I will be using it throughout many places down below in my code."* put all that code in a function. Call that function from the `.done` callback. – Felix Kling Mar 30 '16 at 13:33
  • Rory appreciate that. But, the result, I am planning to use for example 1) as a parameter to another function. 2) within a if-else statement at another point in the flow of the program. If writing all the logic within the callback is the only way, then probably my approach to the whole thing is wrong.. In this case, I need the value in a global variable. What is you suggested way of storing a json response in a global variable then? – Subliminal Hash Mar 30 '16 at 13:35
  • James, that is a very good read.. thank you.. – Subliminal Hash Mar 30 '16 at 13:35
  • Felix, thanks for your suggestion. Is that a "best practise"? to be more specific, I am (trying) loading some settings via getJSON and want to use setting values at relevant places e.g. base_url, default_image etc.. wrapping everything in a function and calling it in callback sounds promising but is it the proper approach you experienced developers adopt? – Subliminal Hash Mar 30 '16 at 13:37
  • It all depends on how the "many places down below in my code" work. It's not a problem to set a global variable from an Ajax callback, as long as the code that needs to access it is executed *after* the callback was executed. Where the code is positioned in the source file doesn't matter. What matters is when it is executed. E.g. in your example, `console.log(myobj)` is executed *before* the Ajax callback was executed. So that doesn't work. But if you put it, lets say, inside a click event handler, and you click element *after* the Ajax callbacks runs, it will work. – Felix Kling Mar 30 '16 at 23:11
  • How you would know that it is the right time to click the element is of course a new problem. That's why executing code that needs access to response from the Ajax callback is "safe": At that point the Ajax callback definitely executed (is executing). – Felix Kling Mar 30 '16 at 23:14

0 Answers0