harryxu
1/8/2018 - 6:25 PM

Async Await Action Creator with Redux Thunk

Async Await Action Creator with Redux Thunk

const BACKEND_URL = 'https://fakeserver.com/api'

export function signUpUser(email, password) {
  return async (dispatch) => {
    try {
      const signUp = await axios.post(`${BACKEND_URL}/signup`, {
        email: email,
        password: password
      })
      
      // signUp.data.token <-- Access token here if the above request finished successfully. 
      dispatch({ type: 'SIGN_UP_SUCCESS' })    
    }
    catch(e) {
      // catch errors here
      dispatch({ type: 'SIGN_UP_ERROR' })  
    }
  }
}