function getFile(url) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
return(xhr.responseText);
}
}
xhr.open('GET', url);
xhr.send();
}
var res = getFile(url);
console.log(res); //Вывело undefined.
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
console.log(xhr.responseText);
document.getElementById('response').innerHTML = xhr.responseText;
someOtherCallback(xhr.responseText);
}
}
function getFile(url) {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open("GET", url);
xhr.onreadystatechange = () => resolve(xhr.responseText);
xhr.onerror = () => reject(xhr.statusText);
xhr.send();
});
}
getFile(url).then((res) => {
console.log(res);
});