【发布时间】:2019-04-23 17:39:24
【问题描述】:
我有一个应用程序可以在渲染时获取一些用户信息。因此,当应用程序首次启动时,它会使用 getUserInformation() 函数获取数据。用户无需手动登录,应用在公司内网中。
export function getUserInformation() {
return function (dispatch) {
getUser()
.then((data) => {
dispatch(
{type: GET_USER_SUCCESS, response: data}
)
})
.catch((error) => {
dispatch(
{type: GET_USER_FAILURE, response: error}
)
})
}
}
现在我想获取应用程序的版本以在整个应用程序中可用。但是只有在用户登录后才能触发 API 调用(因此成功调用了 getUser())。我应该添加
.then(getVersion())
在 getUserInformation() 操作中? 它看起来并不干净,但我不知道如何以不同的方式处理它。
【问题讨论】:
-
好吧,如果
getVersion应该只在getUserInformation之后运行,那么我看不到更好的选择。 -
[offtop] 不是
.then(getVersion())而是.then(getVersion)否则你会得到错误的排序并可能面临竞争条件
标签: javascript reactjs redux promise redux-thunk