I have a code that XMLHttpRequest. It's a object to send request to server and it's asynchronous so when I want to receive the response I need to provide the callback function to onreadystatechange
property of this object. And this function is called after response being received:
function send()
{
XMLHttpRequest req = new...
req.onreadystatechange = answer;
}
function answer()
{
//handling the answer...
}
So it's great but I don't want to use new function to handle answer so I do it anonymous:
function send()
{
XMLHttpRequest req = new...
req.onreadystatechange = function ()
{
//handling the answer...
};
}
But now i want to use the result of send function in another function for example to display result:
display(send())
So how to make this work? Something like:
function send()
{
XMLHttpRequest req = new...
req.onreadystatechange = function ()
{
//handling the answer...
return result; //where result is returned by send function
};
}
Is there any way to do this so that other JS code will still work while this code will handle the response?