Home > Blockchain >  How to avoid api call if argument is null in forKJoin in angular 5?
How to avoid api call if argument is null in forKJoin in angular 5?

Time:04-14

this.hobbies :any[]=[];
this.worklist : any[]=[];
forkJoin(this.personService.addHobbies(this.hobbies),this.professionService.updateWorks(this.worklist))
        .subscribe(res => {});

here i'm calling 2 apis this.personService.addHobbies(this.hobbies) and this.professionService.updateWorks(this.worklist) with forkJoin, but i want to avoid api call this.personService.addHobbies() if this.hobbies is empty list.. only this.professionService.updateWorks(this.worklist) should call if this.worklist is not empty list. Is there any solution to avoid that particular api call if list is null or empty.. Please help

CodePudding user response:

You can use an array in which you add your API calls :

this.hobbies: any[] = [];
this.worklist: any[] = [];
        
const requests = [];
if (this.hobbies.length > 0){
  requests.push(this.personService.addHobbies(this.hobbies));
}
if (this.worklist.length > 0) {
  requests.push(this.professionService.updateWorks(this.worklist));
}

forkJoin(requests).subscribe(res => {});

  • Related