Is it possible to await for dispatch is finish

Viewed 7361

I have 2 actions, and I call from the first action to the second action .... and I need to wait until the second action is finished and only then continue with the action.

// first action
export const getDataFromAdmin = () => {
    return async (dispatch, getState) => {
        dispatch(getDeviceLocation());
        console.log('only after getDeviceLocation is finsih');
        AdminRepository.getDataFromAdminAndEnums(dispatch)
            .then(adminData => {
              //some code 
            })
            .catch(error => {
                console.log(`Splash Error = ${error.message}`);
            });
    };
};


//second action
export const getDeviceLocation = () => {
    return async dispatch => {
        dispatch({ type: actionsType.GET_DEVICE_LOCATION });
        LocationManager.getCurrentPosition()
            .then(location => {
         
                dispatch({ type: actionsType.GET_DEVICE_LOCATION_SUCCESS });
            })
            .catch(error => {
                dispatch({ type: actionsType.GET_DEVICE_LOCATION_ERROR, message: error.message });
            });
    };
};
2 Answers

No, it's not possible because dispatching an action is something like triggering action and action just invoke either middleware or reducer that's it. The action doesn't wait to complete the reducer or middleware. It just invokes and finishes his job.

Ok, so in the end I make async await by pass dispatch as a parmeter to the function.

Related