question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sagas and reducers: order of execution

See original GitHub issue

I supposed that as sagas are middleware they’re executed before the reducers It appears that actually this is not true.

Here’s my example:

  1. 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
    })
  }
  1. 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:closed
  • Created 8 years ago
  • Reactions:18
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

89reactions
yelouaficommented, Feb 23, 2016

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

48reactions
yelouaficommented, Feb 23, 2016

Should certainely be mentioned in the docs

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found