function getData() {
let result;
let xhr = new XMLHttpRequest();
xhr.open('GET', 'http://site1.com');
xhr.send();
xhr.onload = function() {
let xhr2 = new XMLHttpRequest();
xhr2.open('GET', 'http://site2.com');
xhr2.send();
xhr2.onload = function() {
result = xhr.response[0] + xhr2.response[0];
}
}
return result;
}
function promiseWrapper(url) {
return new Promise(resolve => {
const xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.send();
xhr.onload = () => {
resolve(xhr.response);
};
});
}
async function getData() {
const [res1] = await promiseWrapper(url1);
const [res2] = await promiseWrapper(url2);
return res1 + res2;
}
window.myObject = {
urls: ['http://site1.com', 'http://site2.com'],
sum: '',
indx: 0,
add: function(str) {
this.sum += str;
if(++this.indx < this.urls.length) {
this.run();
} else {
alert('Result is ' + this.sum);
}
},
run: function() {
let xhr = new XMLHttpRequest();
xhr.open('GET', this.urls[this.indx]);
xhr.send();
xhr.onload = function() { window.myObject.add(xhr.result[0]); }
}
};
window.myObject.run();