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