-1

I have a http call which when starts calling first few seconds is error 404 until the result is coming to database:

this.http.get(`http://localhost:44301/consentinitiation/${this.qid}`).subscribe(s=>{
 console.log(s);
 },error=>(error));

what i thought is to put it inside while

        shouldCall=true;
        while(shouldCall){
         this.http.get(`http://localhost:44301/consentinitiation/${this.qid}`).subscribe(s=>{
         console.log(s);
         shouldCall=false;
         },error=>(error));}

the problem is the browser crashes with this approach, any idea?

Gaël J
  • 11,274
  • 4
  • 17
  • 32
moris62
  • 983
  • 1
  • 14
  • 41
  • 1
    Does this answer your question? [How to repeat ajax call until success](https://stackoverflow.com/questions/20583302/how-to-repeat-ajax-call-until-success) – Heretic Monkey Aug 17 '21 at 20:05
  • Did you try debugging? What happens if you put a log in the while loop but outside of the Observable? Hint: this is async code, there's no wait between each request.. – Gaël J Aug 17 '21 at 20:06
  • Does this answer your question? [How do i repeat an ajax request until a condition is met with RxJS Observable?](https://stackoverflow.com/questions/43400594/how-do-i-repeat-an-ajax-request-until-a-condition-is-met-with-rxjs-observable) – Gaël J Aug 17 '21 at 20:07
  • @GaëlJ should i write my httpcall inside flatmap? – moris62 Aug 17 '21 at 20:13

1 Answers1

0

Easiest way is to use retry operator which will retry the stream until success

this.http.get(`http://localhost:44301/consentinitiation/${this.qid}`)
.pipe(retry())
subscribe(s=>{
 console.log(s);
 },error=>(error));

You can also add a param to specify max number of retry e.g retry(5)

Fan Cheung
  • 10,745
  • 3
  • 17
  • 39