Skip to content

Instantly share code, notes, and snippets.

@acanimal
Created January 18, 2017 09:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acanimal/786f95727f19ca469df1240c7fe143fd to your computer and use it in GitHub Desktop.
Save acanimal/786f95727f19ca469df1240c7fe143fd to your computer and use it in GitHub Desktop.
Higher Order Actions
const higherOrderAction = (func) => (...args) => (dispatch, ...props) => {
// Do whatever you need.
// Call the wrapped action.
const call = func(undefined, ...args);
// Check if the call returns a function, which means it is an async action
if (call && call.constructor && call.call && call.apply) {
return call(dispatch, ...props);
}
return dispatch(call);
};
const actionStart = () => ({
type: 'ACTION_ONE',
});
const actionFinish = (result) => ({
type: 'ACTION_ONE',
result,
});
const actionAsync = (a, b) => (dispatch) => {
dispatch(actionStart());
dispatch(actionFinish(a+b);
};
export const higherActionStart = higherOrderAction(actionStart);
export const higherActionFinish = higherOrderAction(actionFinish);
export const higherActionAsync = higherOrderAction(actionAsync);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment