0

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)
    });
  }

enter image description here

When this then works, I want to replace the if with the while. If I insert it now it goes into an infinite loop

3
  • why dont you do it like this, ` 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) });` do a recursive call. Commented Dec 24, 2021 at 16:37
  • Perfect @DeepakJha , it works!!! 2 hours racking my brain when the solution was simpler =) . Thank you very much Commented Dec 24, 2021 at 17:13
  • let me put this in answer then :) please accept it ! Commented Dec 24, 2021 at 17:16

2 Answers 2

1

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) });
Sign up to request clarification or add additional context in comments.

Comments

0
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)
});
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.