Eth3rnit3
12/19/2018 - 8:00 AM

Async Action

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)
    }
  }
}