Donmclean
2/10/2017 - 5:36 PM

Redux Observable Handling Async Example

Redux Observable Handling Async Example

const fetchFirst = () => ({ type: FETCH_FIRST });
const fetchSecond = (response) => ({ type: FETCH_SECOND, payload: response });

const fetchFirstManager = (actions, store) =>
  actions.ofType(FETCH_FIRST)
    .switchMap(action =>
      ajax('/first')
        .map(response => fetchSecond(response))
    );

const fetchSecondManager = (actions, store) =>
  actions.ofType(FETCH_SECOND)
    .switchMap(action =>
      ajax('/second')
        .map(response => ({ type: OK_IM_DONE, payload: response }))
    );

store.dispatch(fetchFirst());
// fetchFirst() -> [await response] -> fetchSecond()