Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
T
task_33
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Гульняшкин Евгений
task_33
Commits
abb56005
Commit
abb56005
authored
Oct 06, 2023
by
Гульняшкин Евгений
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
изменил реквест на промис
parent
84ebf1e3
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
16 additions
and
33 deletions
+16
-33
script.js
task_1/script.js
+16
-33
No files found.
task_1/script.js
View file @
abb56005
...
@@ -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
{
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'
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
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment