Skip to content

Instantly share code, notes, and snippets.

@gaurav-gogia
Last active May 27, 2018 18:42
Show Gist options
  • Save gaurav-gogia/b411659baf001cfb49d52137f901f254 to your computer and use it in GitHub Desktop.
Save gaurav-gogia/b411659baf001cfb49d52137f901f254 to your computer and use it in GitHub Desktop.
How do I use async/await functions in actions?
import axios from 'axios';
import {ACTION_TYPES, URL} from '../constants';
export const getAnimals = () => {
//code for use below async function to get the data and use it as payload
return {
type: ACTION_TYPES.GET_ANIMALS,
payload: {}
}
}
const getAnimalsAsync = async () => await axios.get(URL.GET_ANIMAL_LIST);
@gaurav-gogia
Copy link
Author

Update:

export const getAnimals = () => {    
    return async (dispatch) => {
        const {data} = await axios.get(URL.GET_ANIMAL_LIST);
        dispatch({
            type: ACTION_TYPES.GET_ANIMALS,
            payload: data
        })
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment