0

I seem to be unable to make the following work. I simply want to define a function that retrieves JSON files and lets me store them in an object without defing global variables.

So basically, why does this Works:

///Global
var filesaved=[];

///function
function getQuandlData(subset,datatype,token){
var url = "http://www.quandl.com/api/v1/datasets/"+subset+"/AAPL"_"+datatype+".json?auth_token="+token;
console.log(url);
$.getJSON(url, function(data) {
    filesaved=data; console.log("filesaved as "+subset+"/"+$('#ticker').val().toUpperCase()+"_"+datatype,filesaved)
}); 

///use
getQuandlData("RAYMOND","NET_INCOME_Q",Quandl_auth_token);filesaved.data[0][1]

and yet this doesn't

///function
function gQD(subset,datatype,token){
var fs=[];
var url = "http://www.quandl.com/api/v1/datasets/"+subset+"/AAPL_"+datatype+".json?auth_token="+token;
console.log(url);
$.getJSON(url, function(data) {
    fs=data;
}); return fs;
                            }
///use
gQD("RAYMOND","NET_INCOME_Q",Quandl_auth_token).data[0][1];

The variable Quandl_auth_token can be obtained from Quandl.com for free, I'd just like to keep mine private :o

***This is essentially my code, only different with my actual code is that I replace "AAPL" with an input from the HTML.

***I understand that my issue revolves around something with asynchronous functions, is there a way to work around that?

***Also the output for the gQD("RAYMOND","NET_INCOME_Q",Quandl_auth_token) example is [], which

asosnovsky
  • 2,158
  • 3
  • 24
  • 41
  • 1
    do you have an error or something in the output? – martinezjc Aug 08 '14 at 21:52
  • You have some messed up "-s in your sample code. – Etheryte Aug 08 '14 at 21:53
  • you are trying to use a sync workflow by using an async function. you need to bring the action to the data, not the data to the action. you do that with the first one by moving console (an action) to the data (filesaved). – dandavis Aug 08 '14 at 21:56
  • mess ups? where? and I tried different variations for the second example (the one where I define fs inside the function and then return it after I make the call), I usually get an "undefined" or an error. – asosnovsky Aug 08 '14 at 21:56
  • @dandavis not sure what you mean there – asosnovsky Aug 08 '14 at 21:58
  • well, put another way, if you use an async method like getJSON, then you shouldn't use the word "return" in any code making use of that to-be-fetched data. – dandavis Aug 08 '14 at 21:59

1 Answers1

1

If I'm understanding correctly, this is an asynchronous function. When you call gQD it calls $.getJSON which sends a callback that assigns fs=data at some point in the future. In the meantime your function returns, with fs still being [].

When the callback is called, it will still assign fs but now you don't have a handle on it anymore.

Also, there are some inconsistencies and typos in your second code example, e.g. gQD vs getQuandlData.

What you probably want to do is call something from your callback function, because that's the point at which you have a valid fs.

I don't know why the first case should work, other than you got lucky timing-wise.

OldGeeksGuide
  • 2,888
  • 13
  • 23
  • I'll bet his actual code uses `alert()` rather than `console.log()`. That changes the timing so it "works". – Barmar Aug 08 '14 at 22:02
  • I didn't use alert. Sorry, I am just a bit of a newbie with js. I understand some of my problem after googling asynchronous functions. Is there a way to retrieve the JSON and storing it, right away without this time-delay? – asosnovsky Aug 08 '14 at 22:14
  • You can't do anything about the time-delay, that's a function of the network. As far as the asynchronous nature of things, I don't think there's anything you can do about that either; as someone pointed out the A in AJAX means Asynchronous. You could create a callback parameter for getQuandlData so the caller can specify what happens when the JSON is received. – OldGeeksGuide Aug 08 '14 at 23:48