Calling one asynchronous function inside another in abbreviation

I am building a responsive application and using decux-thunk for asynchronous operations. I have two functions getActivities()and createActivity(), and I want to name the first after successfully invoking the latter. But if I put it getActivities()inside a thenblock createActivity(), it just won’t be called (which is confirmed by the absence of console.log (), which I entered in getActivities()). Here are both functions:

export const getActivities = () => dispatch => {
console.log('again');
return axios.get(ENV.stravaAPI.athleteActivitiesBaseEndPoint, autHeaders)
    .then(resp => {
        dispatch({type: actions.GET_ACTIVITIES, activities: resp.data})
    })
    .catch(err => {
        if(window.DEBUG)console.log(err);
    })
};

export const createActivity = data => dispatch => {

dispatch(setLoadingElement('activityForm'));
return axios.post(URL, null, autHeaders)
    .then(resp => {
        if (resp.status === 201) {
            dispatch(emptyModal());
        }
        // I WANT TO CALL getActivities() HERE
        dispatch(unsetLoadingElement('activityForm'));
    })
    .catch(err => {
        if(window.DEBUG) console.log(err.response.data.errors);
        dispatch(unsetLoadingElement('activityForm'));
    });
};

How can I call one inside the other?

+4
source share
2 answers

To call another action from within the same action creator, you just need to dispatchperform an action likedispatch(getActivities())

export const createActivity = data => dispatch => {

    dispatch(setLoadingElement('activityForm'));
    return axios.post(URL, null, autHeaders)
        .then(resp => {
            if (resp.status === 201) {
                dispatch(emptyModal());
            }
            dispatch(getActivities());
            dispatch(unsetLoadingElement('activityForm'));
        })
        .catch(err => {
            if(window.DEBUG) console.log(err.response.data.errors);
            dispatch(unsetLoadingElement('activityForm'));
        });
 };
+5
source
getActivites()

getActivities(). , console.log(). .

, , :

dispatch(getActivities())
0

Source: https://habr.com/ru/post/1692461/


All Articles