【发布时间】:2021-11-09 03:08:48
【问题描述】:
我使用 while 循环多次调用同一个服务,但由于 API 响应延迟,我的数组没有正确更新。我想在 while 循环和所有服务响应完成后调用某些语句。
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);
})
}
回应
Pass1
[]
Pass1
[]
(20) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
(40) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
我希望仅在 while 循环中更新名称,或者在更新名称数组后执行 while 循环之后的语句。请帮我解决给定的问题。
【问题讨论】:
标签: angular typescript rxjs angular8