2

The following call makes like around 800 connection in 5 seconds, I want to make a connection every 5 seconds to reduce the number of connections. If I put retry(5) after 5 attempts it will stop and I may not have a successful result in 5 tries, thats why I need a time here I think.

  this.http.get(`api`)
 .pipe(retry())
 .subscribe(s=>{
   //result
  },error=>(error));

I don't know where should I put interval actually as far as I know it should come after subscribe but it seems this approach is not the right way.

Robse
  • 853
  • 3
  • 14
  • 28
moris62
  • 983
  • 1
  • 14
  • 41

2 Answers2

2

You'd need to use the retryWhen operator with delay operator. Try the following

import { retryWhen, delay } from 'rxjs/operators';

this.http.get(`api`).pipe(
  retryWhen(error => error.pipe(delay(5000))
).subscribe(
  s => { 
    // result
  },
  error => {
    // handle error
  }
);
ruth
  • 29,535
  • 4
  • 30
  • 57
1

I have used similar functionality in one of my angualr application and worked out this way. Check if this helps.

this.subscription = interval(5000).subscribe(x => {
// API call goes here...
       
});

This code continously executes for every 5 seconds once and if you want to stop the timer then

this.subscription.unsubscribe();
Siva Makani
  • 196
  • 2
  • 17
  • **1.** OP's question is about retriggering the API call when it fails. Using `interval()` is a basic polling mechanism. **2.** Instead of nested subscriptions, it's elegant to use higher order mapping operators. See [here](https://stackoverflow.com/a/63685990/6513921). – ruth Aug 18 '21 at 11:12