Created
July 15, 2016 15:54
-
-
Save Kadrian/ea47aff8b4a532f4b67bd6cae6796b5e to your computer and use it in GitHub Desktop.
Redux async actions example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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