Skip to content

Commit fb2cd6c

Browse files
committed
fix: промисы переписаны на asymc/await
1 parent 3a49b9e commit fb2cd6c

File tree

4 files changed

+55
-57
lines changed

4 files changed

+55
-57
lines changed

src/js/fetchRssData.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import axios from 'axios'
22
import { model } from './model/index.js'
33

4-
export const fetchRssData = (link) => {
4+
export const fetchRssData = async (link) => {
55
const proxyUrl = `https://allorigins.hexlet.app/get?disableCache=true&url=${encodeURIComponent(link)}`
6-
return axios
7-
.get(proxyUrl)
8-
.then((response) => {
9-
const data = response.data
10-
return data.contents
11-
})
12-
.catch((error) => {
13-
model.error.handle(error, 'fetch')
14-
throw error
15-
})
6+
7+
try {
8+
const response = await axios.get(proxyUrl)
9+
return response.data.contents
10+
}
11+
catch (error) {
12+
model.error.handle(error, 'fetch')
13+
throw error
14+
}
1615
}

src/js/model/feed.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,14 @@ export const updateState = ({ channel, items }) => {
2525
state.form.inputValue = ''
2626
}
2727

28-
export const add = () => {
28+
export const add = async () => {
2929
const url = state.form.inputValue
30-
return fetchAndParse(url)
31-
.then(({ channel, items }) => {
32-
updateState({ channel, items })
33-
})
34-
.catch((error) => {
35-
// и тут корректное ли это решение
36-
handle(error, state.ui.error === 'noRss' ? 'parse' : 'fetch')
37-
throw error
38-
})
30+
try {
31+
const { channel, items } = await fetchAndParse(url)
32+
updateState({ channel, items })
33+
}
34+
catch (error) {
35+
handle(error, state.ui.error === 'noRss' ? 'parse' : 'fetch')
36+
throw error
37+
}
3938
}

src/js/model/form.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,17 @@ const schema = yup
88
.required('required')
99
.test('no-duplicate', 'exists', value => !state.feedsList.includes(value))
1010

11-
export const validateInput = () => {
12-
return schema
13-
.validate(state.form.inputValue)
14-
.then(() => {
15-
state.ui.status = 'success'
16-
return state.form.inputValue
17-
})
18-
.catch((error) => {
19-
state.ui.status = 'error'
20-
state.ui.error = error.errors.join()
21-
throw error
22-
})
11+
export const validateInput = async () => {
12+
try {
13+
await schema.validate(state.form.inputValue)
14+
state.ui.status = 'success'
15+
return state.form.inputValue
16+
}
17+
catch (error) {
18+
state.ui.status = 'error'
19+
state.ui.error = error.errors.join()
20+
throw error
21+
}
2322
}
2423

2524
export const updateInputValue = (value) => {

src/js/model/update.js

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,37 @@ import { fetchAndParse } from './feed.js'
33
import { addNew } from './post.js'
44
import { handle } from './error.js'
55

6-
export const checkFeeds = () => {
7-
const promises = state.feedsList.map((feed) => {
6+
export const checkFeeds = async () => {
7+
const promises = state.feedsList.map(async (feed) => {
88
state.ui.status = 'pending'
9-
return fetchAndParse(feed)
10-
.then(({ items }) => {
11-
return items
12-
})
13-
.catch((error) => {
14-
state.ui.status = 'error'
15-
handle(error, 'fetch')
16-
return []
17-
})
18-
})
199

20-
return Promise.all(promises).then((results) => {
21-
const newPosts = results.flat()
22-
addNew(newPosts)
10+
try {
11+
const { items } = await fetchAndParse(feed)
12+
return items
13+
}
14+
catch (error) {
15+
state.ui.status = 'error'
16+
handle(error, 'fetch')
17+
return []
18+
}
2319
})
20+
21+
const results = await Promise.all(promises)
22+
const newPosts = results.flat()
23+
addNew(newPosts)
2424
}
2525

26-
export const startFeedChecks = () => {
26+
export const startFeedChecks = async () => {
2727
if (state.feeds.length === 0) {
2828
return
2929
}
30-
checkFeeds()
31-
.then(() => {
32-
setTimeout(startFeedChecks, 10000)
33-
})
34-
.catch((error) => {
35-
console.error(error.message)
36-
setTimeout(startFeedChecks, 10000)
37-
})
30+
31+
try {
32+
await checkFeeds()
33+
setTimeout(startFeedChecks, 10000)
34+
}
35+
catch (error) {
36+
console.error(error.message)
37+
setTimeout(startFeedChecks, 10000)
38+
}
3839
}

0 commit comments

Comments
 (0)