let myPromise = new Promise(function(myResolve, myReject) {
let xhr = new XMLHttpRequest();
let xhr1 = new XMLHttpRequest();
xhr.open(`GET`, `style.css`);
xhr1.open(`GET`, `xmlData.html`);
xhr.onload = function () {
if (xhr.status === 200 && xhr.readyState === 4) {
myResolve(xhr.responseText);
}
else {
myReject("Error");
}
}
xhr1.onload = function () {
if (xhr1.status === 200 && xhr1.readyState === 4) {
myResolve(xhr1.responseText);
}
else {
myReject("Error");
}
}
xhr.send();
xhr1.send();
});
myPromise.then(
function(value) {console.log(value);}
);
myPromise.then(
function(secondValue) {console.log(secondValue);}
);
myPromise.catch(
function (error) {
console.log(error);
}
)
myPromise.catch(
function (error) {
console.log(error);
}
)
Promise.all
:function XMLHttpPromise(method, url, data) {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open(method, url);
xhr.onload = () => {
if (xhr.status === 200 && xhr.readyState === 4)
resolve(xhr.responseText);
else
reject("Error");
}
xhr.onerror = xhr.ontimeout = reject;
xhr.send(data);
})
}
Promise.all([
XMLHttpPromise('GET', `style.css`),
XMLHttpPromise('GET', `xmlData.html`)
]).then(([value, secondValue]) => {
console.log(value);
console.log(secondValue);
}).catch(console.error);