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

parent 84ebf1e3
...@@ -13,24 +13,20 @@ let name_to_find = 'Kazakhstan' ...@@ -13,24 +13,20 @@ let name_to_find = 'Kazakhstan'
const request = (config) => { const request = (config) => {
loader.style.display = 'flex' loader.style.display = 'flex'
const { method, path, success, error } = config; const { method, path, success, error } = config;
return new Promise((resolve, reject) => {
const xml = new XMLHttpRequest(); const xml = new XMLHttpRequest();
console.log(name_to_find, config.path)
xml.addEventListener('load', () => { xml.addEventListener('load', () => {
if (xml.status < 200 || xml.status > 399) { if (xml.status >= 200 || xml.status > 300) {
if (xml.status === 404) { const result = JSON.parse(xml.responseText);
error('No country with such name!'); resolve(result)
}
else {
error('bad response');
}
return;
} }
else if (xml.status === '404') { else {
console.log(xml.status); reject(this.status)
} }
const result = JSON.parse(xml.responseText);
success(result)
}); });
loader.style.display = 'none' loader.style.display = 'none'
xml.addEventListener('error', () => { xml.addEventListener('error', () => {
...@@ -41,6 +37,7 @@ const request = (config) => { ...@@ -41,6 +37,7 @@ const request = (config) => {
}) })
xml.open(method, path); xml.open(method, path);
xml.send(); xml.send();
})
} }
const processResult = (result) => { const processResult = (result) => {
...@@ -57,7 +54,6 @@ const errorHandler = (msg) => { ...@@ -57,7 +54,6 @@ const errorHandler = (msg) => {
const createTree = (data) => { const createTree = (data) => {
console.log(data);
const ul = document.createElement('ul') const ul = document.createElement('ul')
for (item in data) { for (item in data) {
const li = document.createElement('li') const li = document.createElement('li')
...@@ -100,21 +96,6 @@ const createTree = (data) => { ...@@ -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 = () => { const showInfo = () => {
if (userInput.value) { if (userInput.value) {
...@@ -123,10 +104,12 @@ const showInfo = () => { ...@@ -123,10 +104,12 @@ const showInfo = () => {
const config = { const config = {
method: "GET", method: "GET",
path: `https://restcountries.com/v2/name/${name_to_find}`, 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' infoToshow.style.display = 'block'
} }
button.addEventListener('click', showInfo) 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