I have this function that calls a service to get data through api. I would like after the first call, if result is equal to 50, another call is made with updated offset. But this doesn't work. It doesn't go into the arrow function:
result: any[] = []
async getData(v) {
await this.gs.getMaxGifs(v, this.offset).subscribe((response: any) => {
this.result.push(...response.data);
this.offset = 50;
if (this.result.length % 50 == 0) {
console.log('DENTRO')
async () => {
await this.gs.getMaxGifs(v, this.offset).subscribe((response: any) => {
console.log('DENTRO22222')
})
this.result.push(...response.data);
this.offset = 50;
}
}
console.log(this.result.length)
});
}
When this then works, I want to replace the if with the while. If I insert it now it goes into an infinite loop
CodePudding user response:
As commented it worked for sirss,
async getData(v) {
await this.gs.getMaxGifs(v, this.offset).subscribe((response: any) => { this.result.push(...response.data);
this.offset = 50; if (this.result.length % 50 == 0) {
console.log('DENTRO');
this.getData(v);
}
console.log(this.result.length) });
CodePudding user response:
result: any[] = []
getData(v) {
this.gs.getMaxGifs(v,
this.offset).subscribe((response: any) => {
this.result.push(...response.data);
this.offset = 50;
if (this.result.length % 50 == 0) {
console.log('DENTRO')
this.gs.getMaxGifs(v, this.offset).subscribe((response: any) => {
console.log('DENTRO22222')
this.result.push(...response.data);
this.offset = 50;
}
}
console.log(this.result.length)
});
}
Or if you want to do this recuresive you can do it like this
result: any[] = []
getData(v) {
this.gs.getMaxGifs(v,
this.offset).subscribe((response: any) => {
this.result.push(...response.data);
this.offset = 50;
if (this.result.length % 50 == 0) {
this.getData(v)
}
console.log(this.result.length)
});
}