Skip to content

Commit

Permalink
refactor: will throw http error and re-fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
little-buddy committed Dec 23, 2023
1 parent 5200482 commit b0e4623
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 21 deletions.
7 changes: 4 additions & 3 deletions src/pages/news/children/recommend/sage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ import {
import { RootState } from '@/store/index'
import { getSongList, getBanner, getRecommendSongList } from './api/index'
import { Song } from '@/interface/index'
import { wrapperReFetch } from '@/utils'

export const actions: ActionTree<RecommendState, RootState> = {
async [RecommendActions.SET_ACTION_BANNERS]({ commit }) {
const banners = await getBanner(0)
const banners = await wrapperReFetch(() => getBanner(0))
commit(RecommendMutations.SET_BANNERS, banners)
},
async [RecommendActions.SET_ACTION_SONG_LIST]({ commit }) {
const result = await getSongList(14)
const result = await wrapperReFetch(() => getSongList(14))
commit(RecommendMutations.SET_SONG_LIST, result)
},
async [RecommendActions.SET_ACTION_RECOMMEND_SONG_LIST]({ commit }) {
const result = await getRecommendSongList()
const result = await wrapperReFetch(getRecommendSongList)
commit(RecommendMutations.SET_SONG_LIST, result)
}
}
Expand Down
34 changes: 16 additions & 18 deletions src/utils/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,31 +46,29 @@ export function get<T>(
params?: unknown,
options?: HttpConfig
): Promise<T> {
return http
.get(url, {
params,
...options
})
.catch(e => {
console.error(e)
return e
})
return http.get(url, {
params,
...options
})
// .catch(e => {
// console.error(e)
// return e
// })
}

export function post<T>(
url: string,
data?: unknown,
options?: HttpConfig
): Promise<T> {
return http
.post(url, {
data,
...options
})
.catch(e => {
console.error(e)
return e
})
return http.post(url, {
data,
...options
})
// .catch(e => {
// console.error(e)
// return e
// })
}

export default http
15 changes: 15 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,3 +324,18 @@ export const measureImg = (source: string) => {
export const overNum = (num: number) => {
return formatCount(num)
}

export const wrapperReFetch = async (
asyncGet: (...args: any) => Promise<any>
) => {
try {
const res = await asyncGet()
return res
} catch (error) {
return new Promise(resolve => {
setTimeout(async () => {
resolve(await wrapperReFetch(asyncGet))
}, 1000)
})
}
}

0 comments on commit b0e4623

Please sign in to comment.