If you need the request to be over before calling the dispatch
function, you could call it in the promise resolve callback (then
), as follows:
return (dispatch) => {
return axios.post(baseUrl + 'v1/drivers/alldriver', {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
})
.then(response => {
console.log('jj')
console.log(response)
// here call whatever you want to do after a succesful request
dispatch({
type: FETCH_ALLDRIVER_DATA_START
})
})
.catch(error => {
throw (error);
});
};
Bear in mind that this is still an async call, but with code beign executed after the request ended successfully.
If you really want to make a sync call, use async
and await
like so:
return async (dispatch) => {
const response = await axios.post(baseUrl + 'v1/drivers/alldriver', {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
});
dispatch({
type: FETCH_ALLDRIVER_DATA_START
})
};
Altough I dont't knoe how redux
will behave with a sync call.
I'm not sure what you are trying to achieve and why you would need the request call to be syncrhonous. If your components are rendering without data, or something like that, the solution must be somewhere else and not in this call.
Hope it helps.