I have a function which makes an ajax request (see code below), how do I return the response?
Code:
const ajaxreq = (url) => {
    let xhttp = new XMLHttpRequest();
    let res = "";
    xhttp.onreadystatechange = _ => {
      if (this.readyState === 4) {
        res = this.responseText
      }
    };
    xhttp.open("GET", url, true);
    xhttp.send();
}
I have tried placing the return statment inside the xhttp.onreadystatechange, but this doesn't make the function ajaxreq return that value, only the anonymous function. If I place the return statement after xhttp.send();, then the request doesn't have enough time to fulfill and the function just returns an empty string.
How can I solve this problem?
