@@ -3,36 +3,37 @@ import { fetchAndParse } from './feed.js'
33import { addNew } from './post.js'
44import { 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