Created
December 19, 2018 08:00
-
-
Save Eth3rnit3/0ad5004e7970a942376bd71361671bc2 to your computer and use it in GitHub Desktop.
example of an asynx action with redux
This file contains hidden or 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
| import axios from 'axios' | |
| export function fetchCategories(){ | |
| return async dispatch => { | |
| function onSuccess(response){ | |
| dispatch({type: 'FETCH_CATEGORIES', payload: response }) | |
| return response | |
| } | |
| function onError(error){ | |
| dispatch({type: 'ERROR_GENERATED', error }) | |
| return error | |
| } | |
| try { | |
| const response = await axios.get('/categories') | |
| return onSuccess(response) | |
| } | |
| catch(err){ | |
| return onError(err) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment