I'm trying to utilize async/await in my React app. From what I've been reading in this code the function should run first and then the console.log will wait for the function to finish...
async postSelectedHandler() {
await this.getInServiceVenues();
console.log('Updated In Service 2: '+this.state.venuesInService);
}
Maybe I'm wrong but what I'm trying to do is make sure the function runs first before the console.log runs but it's still running asynchronously.
Code for getInServiceVenues...
getInServiceVenues = () =>{
this.setState({ loading: true });
let bodyInService = [];
let iterationInService = 0;
let RequestingNetworkOperatorList = [];
let updatedVenuesInService = [];
axios.post( '/propertymanagement/listVenues', bodyInService,
{
headers: {}
})
.then( response => {
if (this._isMounted) {
this.setState({loading: false});
this.setState({venues: []});
const venuesInService = response.data.InServiceVenueList;
let venueIdInService = null;
bodyInService.push(venuesInService);
bodyInService.forEach(val=>{
venueIdInService = Object.keys(bodyInService[0]);
//console.log(venueId);
})
if(this.state.venuesInService!==[]){
this.setState({venuesInService:[]});
}
venueIdInService.forEach(val=>{
updatedVenuesInService = bodyInService.map(venueInService => {
return {
...venueInService[venueIdInService[iterationInService]],
iterationInService,
RequestingNetworkOperatorList
}
});
this.setState({
venuesInService:[...this.state.venuesInService, updatedVenuesInService]
});
iterationInService = iterationInService + 1;
})
console.log('Updated In Service 1: '+this.state.venuesInService);
}} )
.catch(error => {
console.log(error);
this.setState({error: true});
});
}