Sagas and reducers: order of execution
See original GitHub issueI supposed that as sagas are middleware they’re executed before the reducers It appears that actually this is not true.
Here’s my example:
- reducer that stores the returnUrl erases it after the successful login:
[at.LOGIN.SUCCESS]: (state, payload) => {
// (1)
const token = payload.access_token
return state.merge({
token,
returnUrl: undefined
})
}
- I have a saga that is supposed to intercept the
LOGIN.SUCCESS
action and do the redirect:
function* handleLogin() {
// (2)
const returnUrl = yield select(state => state.auth.get('returnUrl') || '/')
yield put(connectSocket())
}
const loginSuccess = sagaDaemon(LOGIN.SUCCESS, handleLogin)
where sagaDaemon
is our helper method:
const sagaDaemon = (actionType, taskSaga) => {
return function* daemon() {
while (true) {
const nextAction = yield take(actionType)
// (3)
yield fork(taskSaga, nextAction.payload, nextAction.meta)
}
}
}
By placing debugger
statements in places (1), (2), and (3) I see, that first the reducer is called, then the master saga gets the control, and then the forks task saga.
So, questions:
- it this behaviour determined?
- is it possible / could it be possible to control it?
In many cases we actually want our sagas to happen after the reducers (currently we ensure this with short delay effects), but certainty is needed. In this particular case the order is not a big deal (I will refactor it slightly), but again I’d like to know that the observed behavior is guaranteed (or how to deal with it if it’s not).
Issue Analytics
- State:
- Created 8 years ago
- Reactions:18
- Comments:14 (5 by maintainers)
Top Results From Across the Web
Understanding Redux Saga: From action creators to sagas
“A middleware is a piece of code that is executed after an action is dispatched but before reaching the reducer”. It's the opposite...
Read more >API Reference - Redux-Saga
Creates an Effect description that instructs the middleware to wait for a specified action on the Store. The Generator is suspended until an...
Read more >Redux-Saga: Is there a guaranteed order of saga execution?
When your action is dispatched, all sagas that have yielded a take effect matching the action will be resumed at once. If an...
Read more >The power of Redux-Saga - Medium
The reducer is a pure function that takes the previous state and an action, and returns the next state. It's very important that...
Read more >Introduction to Redux Saga | LoginRadius Blog
A Redux middleware lies between an action and a reducer. This enables actions to contain something else other than a plain object, as...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
the middleware first hit the reducers then after dispatches to sagas. Since reducers are always synchronous the answer is yes sagas always take actions after the reducers.
The order of what happens after is entirely defined by how you implement your flow. In the example above the master of course takes the action first and then forked task
Should certainely be mentioned in the docs