For the most part my code is working, but I'm apparently misunderstanding something about AJAX or and/or how to have the response data stored in the variable as I want.
var stored_data = getData(url, function(data){
return data;
});
I'm expecting stored_data = data
, but that's not happening.
If I replace return data
with console.log(data)
, it's clear that I am receiving the data I expected.
I'm a bit confused why data is not being assigned to stored_data
function getData(source, callback) {
var xhr = new XMLHttpRequest();
var result= '';
xhr.open("GET", source);
xhr.responseType = "document";
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200){
result = xhr.responseXML.getElementsByTagName('pre')[0].innerHTML;
callback(result);
}
}
xhr.send();
}
Am I doing this correctly in order to set the value of stored_data
, to the results of the AJAX request?
NOTE: I asked this question AFTER reading the one I'm asserted as DUPING. The other question has a lot of conceptual detail. MY question is really simple, in terms of what I am attempting to resolve.
What am I misunderstanding about assigning the value of a function calls results to a variable? It feels like it's something ridiculously simple, and I'm not seeing it.
Apparently, I'm confusing something about variables and function assignment.
E.g.
var somevariable = sum(9,1);
function sum(a, b){return a + b}
console.log(somevariable) // returns 10
So, with that understanding,
If I call my getData
function and assign it to a variable
The data should land in that container right?
And using a callback is fine if I console.log()
But I can't use the info if it's logged to console.
So am I misunderstanding variable = synchronous_result
versus variable = async_result
How is it that I can have my variable resolve to the retrieved result from an async call, using VANILLA JS and a callback? What am I misunderstanding, and how do come to an understanding of what seems like a very simple question?
I comprehend asynchronous. So how do I get the data to resolve? I don't need anything done to the data other than to retrieve it, and then store it?
RESOLUTION: FOR THE SAKE OF OTHERS HAVING A SIMILAR MISUNDERSTANDING Borrowing off the concept in the post that this one is purportedly duplicating:
I have an object with a property that I want to populate with data from a URL. This requires an AJAX operation.
datastore.info
is the property and object.
The resolution was in this code singature:
datastore.info = getINFO(url, function(data){datastore.info = data;});
That's it.
For me it is counter-intuitive. I was expecting the function call to result in a value, but with asynchronous code, the assignment does not occur as a result of the function call. It is a delayed action that comes from the asynchronous step.
As a result, the assignment occurs when the callback is asserted. This logic is counter to synchronous coding, and it took this particularly simple exercise to see that clearly.
Self-learning is a bitch when the help you think you have is not actually there. But spending 8 hours sussing out a one-line problem does make one appreciate their own persistence.
Figuring this out helped me "level up" on the concept of callbacks, asynchoronous operations, and promises as a powerful tool for "out of time" actions.