In a function I loop, in which I’m making fetch calls. When all calls are finished, I need to save the values in a variable, but that’s not being possible for me to be asynchronous to the calls.
getReportsGroup(bukrs){
//TOTAL OF A PROJECT GROUP
fetch('api/Reports/GetDataIRPA?SETCLASS=' + this.state.SETCLASS, {
method: 'GET',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
}
})
.then(response => response.json())
.then(data => {
this.setState({
reportIRPA: data
});
});
}
getTotalProjects(){
//Browse selected projects
for (var i = 0; i < this.state.selectProjects.length; i++) {
this.getReportsGroup(this.state.selectProjects[i].bukrs);
}
console.log('finish all fetch');
}
The getTotalProjects function is performing a loop in which getReportsGroup is called (fetch is made here). At the end of all fetchs I need to display a message in getTotalProjects. Currently, being asynchronous, it performs the console.log('finish all fetch') before finishing all fetch.