Skip to content

Instantly share code, notes, and snippets.

@Eth3rnit3
Created December 19, 2018 08:00
Show Gist options
  • Save Eth3rnit3/0ad5004e7970a942376bd71361671bc2 to your computer and use it in GitHub Desktop.
Save Eth3rnit3/0ad5004e7970a942376bd71361671bc2 to your computer and use it in GitHub Desktop.
example of an asynx action with redux
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