Skip to content

Instantly share code, notes, and snippets.

@Kadrian
Created July 15, 2016 15:54
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 Kadrian/ea47aff8b4a532f4b67bd6cae6796b5e to your computer and use it in GitHub Desktop.
Save Kadrian/ea47aff8b4a532f4b67bd6cae6796b5e to your computer and use it in GitHub Desktop.
Redux async actions example
const defaultError = (type, error) => (
{
type,
error: true,
payload: {
message: error
},
}
);
const defaultSuccess = (type, results) => (
{
type,
payload: {
data: results,
receivedAt: Date.now()
}
}
);
const getDataStart = () => ({ type: 'GET_DATA_START' });
const getDataError = (err) => defaultError('GET_DATA_ERROR', err);
const getDataSuccess = (res) => defaultSuccess('GET_DATA_SUCCESS', res);
export const getData = () => {
return (dispatch) => {
dispatch(getDataStart());
return api.getData()
.then(
r => dispatch(getDataSuccess(r)),
e => dispatch(getDataError(e))
);
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment