0

I need to execute http get calls sequentially. This How to chain Http calls in Angular2 has a very good explanation of how to do that. However whenever I revisit the route my observables are called 2 times more after every visit. So many repeat http calls is not good. How do I make sure that calls are made only once.

unsubscribing on destroy does not seem to help either.

ngOnInit() {
    let counter1 = 0, counter2 = 0;
    console.log('MyBiraComponent ngOnInit');
    let uk = this.us.get();
    this.u = uk.map(person => {
        console.log('line 47, Person Id: ', ++counter1);
        return person.id;
    }).flatMap(id => {
        console.log('line 48 - Person id: ', person.id);
        return this.bu.get(id);
    }).subscribe(res => {
        console.log(res);
        });
}




ngOnDestroy() {
    this.u.unsubscribe();
    console.log('MyBiraComponent Destroyed');
  }

first visit to route

enter image description here

2nd visit to route

enter image description here

3rd visit to route

enter image description here

Community
  • 1
  • 1
Prabhat
  • 4,066
  • 4
  • 34
  • 41
  • 1
    Can you add you `.us(...)` code? Specifically the `.get()` function – Meir Dec 11 '16 at 15:39
  • 1
    Ah. Thanks a lot @Meir for pointing me in right direction. I was using a ReplaySubject. Changing it to Subject solved the issue. – Prabhat Dec 11 '16 at 15:49
  • 1
    That's what I suspected. You can always use `take(1)` to make sure your subscription terminates after the operation – Meir Dec 11 '16 at 15:51

0 Answers0