This is not a duplicate question.
It is a request help finding technical conclusion, that is not addressed in How do I return the response from an asynchronous call?
If .then() is not resolving the Promise, how do I track down the problem at that point? It seems like I missed something technically, not conceptually.
This function retrieves data from a URL:
async function getINFO(source, callback) {
var xhr = new XMLHttpRequest();
xhr.open("GET", source);
xhr.responseType = "document";
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200){
var text = xhr.responseXML.getElementsByTagName('pre')[0].innerHTML;
console.log(text);
callback(text);
}
}
xhr.send();
}
Externally defined callback:
function asyn_return(input){
console.log(input);
return input;
}
The data comes back perfectly in AJAX.
I want to populate an attribute member (datastore.info
) with the data.
info
is a URL string.
This is the place in the code where I use my AJAX data retrieval function:
if (typeof(info) === 'string'){
// If I have a URL retrieve the data and add it to the datastore object.
datastore.info = getINFO(info, asyn_return).then(data =>{
console.log(data);
return data;
});
//console.log(datastore.info);
} else {
datastore.info = "";
}
console.log(datastore.info);
console.log(data)
returns the data I expect, but datastore.info
is not populated:
{item1: "data", item2:"data", info: {} }
Not sure what I'm missing technically to have this resolve to the result.
Once I have the promise (the wrapper), what triggers it to resolve? And when that is not happening, what is the likely issue?
Thanks to anyone who can help.