I understand how to use functions and also asynchronous functions with binding applying and calling. But I'm searching for a solution for the following for a long time.
To clarify why this is not a duplicate: I know how asynchronous calls work. I also understand why a variable isn't updated directly (due to the time difference between the start and the end of the call). My question simplified: is it possible to return to the original function and populate the variable?
Consider the following JavaScript pseudo code:
var returnedData = loadDataThroughAjax(url);
function loadDataThroughAjax(url)
{
//here fires an xhrrequest using the specific url
return xhr.readystatefunction = function()
{
if (this.reaystate == 4)
{
return this.responseText;
}
}
}
I would like the responsetext
of the call to be loaded in the var returnedData
in a single thread. I looked at some jquery
code and there it looked like you could pull this of, or is the thing I'm asking impossible for an asynchronous call?