Skip to content

Instantly share code, notes, and snippets.

@chodorowicz
Last active July 18, 2016 10:43
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 chodorowicz/75d2bb29f347595f74bf96bc7dac1608 to your computer and use it in GitHub Desktop.
Save chodorowicz/75d2bb29f347595f74bf96bc7dac1608 to your computer and use it in GitHub Desktop.
redux middlewares
/**
* bare middleare
*/
export default function({ dispatch }) {
return next => action => {
console.log(action);
// send this action to next middleware or reducers if that's the last middleware
next(action);
}
}
/**
* simple promise middleware
* https://www.udemy.com/react-redux-tutorial/learn/v4/t/lecture/4709458
*/
export default function({ dispatch }) {
return next => action => {
// If action does not have payload
// or, the payload does not have a .then property
// we dont care about it, send it on
if (!action.payload || !action.payload.then) {
return next(action);
}
// Make sure the action's promise resolves
action.payload
.then(function(response) {
// create a new action with the old type, but
// replace the promise with the reponse data
const newAction = { ...action, payload: response };
dispatch(newAction);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment