0

I have to call thisvariable as an argument from another function but I cannot seem to do it because it is not returned. What do i do?

function Getthisvarible()
{
    var thisvariable;
    var Request = new XMLHttpRequest();
    var input = document.getElementById("SearchBox").value;
    var url = "urlstring"

    Request.open("GET",url,true);

    Request.setRequestHeader("Authorization", "Bearer <apikey>");
    Request.setRequestHeader("Accept", "application/vnd.api+json");

    Request.send();

   Request.onreadystatechange = function () {
        if (this.readyState == 4) {
            var jsonparsed = JSON.parse(Request.responseText);
            var thisvariable = jsonparsed.data[0];

        }

    }
    return thisvariable;


}

function Usethisvariable(somevariable)
{}

Usethisvariable(Getthisvariable());
  • You need to use callbacks in order to make this work...remember that your are doing an `async` request – Hackerman Jun 28 '18 at 21:16
  • seems like `thisvariable` is still undefined when you call `return thisvariable;`. You need to make the other function run only when the request finish – Liora Haydont Jun 28 '18 at 21:16

0 Answers0