Skip to content

Question on the order of actions, especially async actions. #602

@kwangkim

Description

@kwangkim

I am a beginner of redux and I still do not understand how to manage the order of actions.

Let A be a pure action which requests to add 1 to a state. And B be an async action which requests to multiply 2 with the same state after 5 seconds.
If the initial state is 2 and I send the action B and A immediately, I expect that the state is 5 since 2*2+1=5.
But my code in Github which is slightly modified from the count example shows 6, which means that the store handled A first instead of B.
I installed it in http://redux-kwangkim.c9.io/
(It may not work, since I am using a free account and they close VM of free accounts frequently.)

//Action
export function multiplicationby2() {
  return {
    type: MULTIPLICATIONBY2_COUNTER
  };
}
export function multiplicationby2Async(delay = 3000) {
  return dispatch => {
    setTimeout(() => {
      dispatch(multiplicationby2());
    }, delay);
  };
}
//Reducer
import { INCREMENT_COUNTER, DECREMENT_COUNTER, MULTIPLICATIONBY2_COUNTER } from '../actions/counter';

export default function counter(state = 0, action) {
  switch (action.type) {
  case INCREMENT_COUNTER:
    return state + 1;
  case DECREMENT_COUNTER:
    return state - 1;
  case MULTIPLICATIONBY2_COUNTER:
    return state*2;  
  default:
    return state;
  }
}

Could you help me to fix my mistake?
Thank you.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions