0

Hi I'm new to angular and I want to know what is the right way of repeating GET request based on the resulting data.

item.service.ts

ScanItems():Observable<any>{
    return this.http.get<any>(url)     
}

item.component.ts

GetScannedItems(){
   this.ScanSubscription = this.itemService.ScanItems()
    .subscribe(
       data => {
         RDate = new Date(data.Date).getTime();
         const NDate = new Date().getTime();
         const Diff = RDate - NDate
         if(Diff > (-300000)){
           console.log('Valid')
         }
         else {
           // Repeat Request Here!!!
         }
       },
       error => console.error(error),
       () => this.ScanSubscription.unsubscribe()
   )
}
Sagar Zala
  • 4,854
  • 9
  • 34
  • 62

1 Answers1

0

instead of handling in component, you can do this in service with retry operator.

GetScannedItems(){
   this.ScanSubscription = this.itemService.ScanItems()
    .pipe(
      map(
       data => {
         RDate = new Date(data.Date).getTime();
         const NDate = new Date().getTime();
         const Diff = RDate - NDate
         if(Diff > (-300000)){
           return of(true);
         }
         else {
            return Observable.throw('retry');
           // Repeat Request Here!!!
         }
       }),
       delay(500),
       retry()
   )
}
Suresh Kumar Ariya
  • 9,516
  • 1
  • 18
  • 27