Demo entry 6762334

samples

   

Submitted by anonymous on Oct 12, 2018 at 00:30
Language: JavaScript. Code size: 1.1 kB.

/* With callbacks */
const doLastThing = (x) => createRequestAction({
  /* etc. */
});

const doNextThing = createRequestAction({
  onSuccess: (data, dispatch) => {
    dispatch(doLastThing(data))
  },
});

export const doFirstThing = createRequestAction({
  onSuccess: (data, dispatch) => {
    dispatch(doNextThing(data));
  },
});

/* With coordinating action creators */
// none of these have `onSuccess` defined
const doFirstThing = createRequestAction({ /* ... */ });
const doNextThing = createRequestAction({ /* ... */ });
const doLastThing = createRequestAction({ /* ... */ });

export const doSomeWork = data => dispatch => (
  dispatch(doFirstThing(data))
    .then(firstData => dispatch(doNextThing(firstData)))
    .then(nextData => dispatch(doLastThing(nextData)))
);

export const doOtherWork = data => dispatch => (
  dispatch(doFirstThing(data))
    .then(firstData => dispatch(doLastThing(firstData)))
);

export const doHigherLevelWork = () => (dispatch, getState) => {
  const dataFromState = selectData(getState());
  return dispatch(doSomeWork(dataFromState))
    .then(someWorkData => dispatch(doMiscThing(someWorkData)));
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).