Skip to content

Instantly share code, notes, and snippets.

@nkt
Created March 7, 2016 20:04
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 nkt/6f7109f860e564d5bf22 to your computer and use it in GitHub Desktop.
Save nkt/6f7109f860e564d5bf22 to your computer and use it in GitHub Desktop.
import isFunction from 'lodash/isFunction';
function createAsyncAction(type, promise) {
return (dispatch) => {
dispatch({
type,
meta: {
pending: true
}
});
promise.then(
(payload) => isFunction(payload) ? payload(dispatch) : dispatch({type, payload}),
(payload) => dispatch({type, payload, error: true})
);
};
}
export default createAsyncAction;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment