изменил реквест на промис

parent 84ebf1e3
......@@ -13,24 +13,20 @@ let name_to_find = 'Kazakhstan'
const request = (config) => {
loader.style.display = 'flex'
const { method, path, success, error } = config;
return new Promise((resolve, reject) => {
const xml = new XMLHttpRequest();
console.log(name_to_find, config.path)
xml.addEventListener('load', () => {
if (xml.status < 200 || xml.status > 399) {
if (xml.status === 404) {
error('No country with such name!');
if (xml.status >= 200 || xml.status > 300) {
const result = JSON.parse(xml.responseText);
resolve(result)
}
else {
error('bad response');
reject(this.status)
}
return;
}
else if (xml.status === '404') {
console.log(xml.status);
}
const result = JSON.parse(xml.responseText);
success(result)
});
loader.style.display = 'none'
xml.addEventListener('error', () => {
......@@ -41,6 +37,7 @@ const request = (config) => {
})
xml.open(method, path);
xml.send();
})
}
const processResult = (result) => {
......@@ -57,7 +54,6 @@ const errorHandler = (msg) => {
const createTree = (data) => {
console.log(data);
const ul = document.createElement('ul')
for (item in data) {
const li = document.createElement('li')
......@@ -100,21 +96,6 @@ const createTree = (data) => {
}
const createTreefromObject = (data) => {
const ul = document.createElement('ul')
for (item in data) {
const li = document.createElement('li')
if (Array.isArray(data[item])) {
li.append(`${item}: `)
const innerUl = createTree(data[item])
li.append(innerUl)
} else {
li.append(`${item}: ${data[item]}`)
}
ul.append(li)
}
return ul
}
const showInfo = () => {
if (userInput.value) {
......@@ -123,10 +104,12 @@ const showInfo = () => {
const config = {
method: "GET",
path: `https://restcountries.com/v2/name/${name_to_find}`,
success: processResult,
error: errorHandler
}
request(config);
request(config)
.then(processResult)
.catch(errorHandler)
infoToshow.style.display = 'block'
}
button.addEventListener('click', showInfo)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment