我有2个动作,我从第一个动作调用到第二个动作.....,我需要等到第二个动作完成后才能继续操作。
// 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 });
});
};
};
答案 0 :(得分:0)
不,这是不可能的,因为调度动作类似于触发动作,而动作仅调用中间件或reducer。该操作不会等待完成reducer或中间件。它只是调用并完成他的工作。
答案 1 :(得分:0)
好,所以最后我通过传递调度作为函数的参数使异步等待。