0

Why do() function is not invoked ?

export class AppComponent implements OnInit {
  name = 'Angular 5';
  events = [];
  existingEvents = [1,2,3,4,6]

  ngOnInit() {
    let i = 0;
    this.events.push('Initial');
    Observable.of(1,2,3,4,5)
    .do(() => { 
      // this block is not executing - why ?
      console.log(this.existingEvents[i]); 
      this.events.push(this.existingEvents[i]);
      i++;
    });
  }
}

Here is a stackblitz for you.

bhantol
  • 9,368
  • 7
  • 44
  • 81
  • 7
    Because nothing subscribes to the observable. It will not emit if it's not subscribed to. – Explosion Pills Mar 08 '18 at 21:31
  • That fixed the issue – bhantol Mar 08 '18 at 21:33
  • 2
    Side note: You should consider switching to [Pipeable Operators](https://github.com/ReactiveX/rxjs/blob/master/doc/pipeable-operators.md). See also [In Angular rxjs when should I use pipe vs map](https://stackoverflow.com/a/47552482/1260204) – Igor Mar 08 '18 at 21:50
  • I am about to delete this question unless anyone of you wants to add the answer. Will give it few hours I guess. – bhantol Mar 09 '18 at 15:59

0 Answers0