admin管理员组文章数量:1180536
Redux saga noob here.
I need to create a saga that loads the initial state for the redux store from my API server.
This involves using two async sagas: getCurrentUser
and getGroups
.
I need to issue these ajax requests in parallel and wait for the GET_CURRENT_USER_SUCCESS
and GET_GROUPS_SUCCESS
actions before issuing the pageReady
action which tells the UI it's time to render the react components.
I came up with a hacky solution:
function * loadInitialState () {
yield fork(getCurrentUser)
yield fork(getGroups)
while (true) {
yield take([
actions.GET_GROUPS_SUCCESS,
actions.GET_CURRENT_USER_SUCCESS
])
yield take([
actions.GET_GROUPS_SUCCESS,
actions.GET_CURRENT_USER_SUCCESS
])
yield put(actions.pageReady())
}
}
The problem with this code is that if for some reason GET_GROUPS_SUCCESS
is issued twice, the pageReady
action will be called to early.
How can I get redux saga to wait for GET_GROUPS_SUCCESS
and GET_CURRENT_USER_SUCCESS
to happen at least once in any order?
Redux saga noob here.
I need to create a saga that loads the initial state for the redux store from my API server.
This involves using two async sagas: getCurrentUser
and getGroups
.
I need to issue these ajax requests in parallel and wait for the GET_CURRENT_USER_SUCCESS
and GET_GROUPS_SUCCESS
actions before issuing the pageReady
action which tells the UI it's time to render the react components.
I came up with a hacky solution:
function * loadInitialState () {
yield fork(getCurrentUser)
yield fork(getGroups)
while (true) {
yield take([
actions.GET_GROUPS_SUCCESS,
actions.GET_CURRENT_USER_SUCCESS
])
yield take([
actions.GET_GROUPS_SUCCESS,
actions.GET_CURRENT_USER_SUCCESS
])
yield put(actions.pageReady())
}
}
The problem with this code is that if for some reason GET_GROUPS_SUCCESS
is issued twice, the pageReady
action will be called to early.
How can I get redux saga to wait for GET_GROUPS_SUCCESS
and GET_CURRENT_USER_SUCCESS
to happen at least once in any order?
1 Answer
Reset to default 36I think you want the all
effect
function * loadInitialState () {
// start loading state...
yield all([
take(actions.GET_GROUPS_SUCCESS)
take(actions.GET_CURRENT_USER_SUCCESS)
]);
yield put(actions.pageReady())
}
本文标签:
版权声明:本文标题:javascript - How can I get redux-saga to wait for two actions to happen at least once in any order? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738212115a2068888.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论