I am calling same service multiple times in angular using while loop but due to delay in response of API my array is not updating properly. I want to call certain set of statement once the while loop & all the services response are completed.
GetData(){
var xyz = [];
var pqr = [];
this.startRow = 0;
this.names=[];
while(this.startRow <= this.num)
{
this.getEmpData();
this.startRow = this.startRow + this.countRow;
console.log("Pass1");
console.log(this.names);
}
//due to delay in API response name array is coming as empty
this.names.forEach(value => {
xyz.push(value);
});
}
getEmpData()
{
this.empService.getEmpData(this.startRow,this.countRow,this.id,this.search).subscribe(data => {
this.resp = data;
this.names = this.names.concat(this.resp);
console.log(this.names);
})
}
Response
Pass1
[]
Pass1
[]
(20) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
(40) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
I want the names to be updated in the while loop only or the statement after the while loop to be executed after once the name array is updated. Please help me out with the given issue.