【发布时间】:2019-04-25 19:24:00
【问题描述】:
例如比较下面的两个代码,第一个使用 async/await,另一个使用 .then 调用 axios。
推荐什么代码?
const BASE_URL = "https://jsonplaceholder.typicode.com"
// async await syntax
export const fetchPosts = () => async dispatch => {
const response = await axios.get(BASE_URL + "/posts")
dispatch({ type: "FETCH_POSTS", payload: response })
}
// using .then instead
export const fetchPosts2 = () => dispatch => {
axios.get(BASE_URL + "/posts").then(response =>
dispatch({
type: "FETCH_POSTS",
payload: response
})
)
}
【问题讨论】:
标签: redux axios redux-thunk