This isn't a repeat query.
I'm seeking assistance in locating a technical solution that hasn't been covered in the post How do I return the response from an asynchronous call?
If .then() doesn't resolve the Promise, how can I pinpoint the issue at that stage? It appears I may have overlooked something from a technical perspective, rather than conceptually.
Here is a function that fetches 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();
}
An externally defined callback:
function asyn_return(input){
console.log(input);
return input;
}
The retrieved data functions perfectly using AJAX.
I want to populate an attribute member (datastore.info
) with this data.
info
is a string containing a URL.
Below is where I execute my AJAX data retrieval function in the code:
if (typeof(info) === 'string'){
// If there's 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)
displays the expected data, but datastore.info
remains empty:
{item1: "data", item2:"data", info: {} }
I'm unsure of what I might be overlooking technically for this to not result in the desired outcome.
After obtaining the promise (the wrapper), what triggers its resolution? And if that isn't happening, what could the probable issue be?
A big thanks to anyone willing to offer assistance.