0

I've been playing with creating my own observables and want to create an observable that returns the 3 times table starting from 3 indefinitely, one every second. However when I try it with this code it freezes the UI and never completes. Could someone explain why this is happening?

I'm still unsure what the subscriber function actually does and what happens to the observable once it hits an observer.next.

 LogThreeTimesTableEveryTwoSeconds(): void {

    let currentValue = 0;

    function someSubscriber(observer: Observer<number>) {
      while (currentValue < 10) {
        setTimeout(() => {
          currentValue += 3;
          observer.next(currentValue);
        }, 1000);
      }
    }

    let anObservable = new Observable(someSubscriber);

    anObservable.subscribe((x) => console.log(x));
 }
user14216042
  • 117
  • 7
  • The while loop will literally never end – Roberto Zvjerković Apr 13 '21 at 16:44
  • Seems that you need to add observeOn and subscribeOn, as I understand, you need to freeze another thread, I'm I right? Is it rx library? – Atamyrat Babayev Apr 13 '21 at 16:48
  • The question linked by Ivar is going to be what you are looking for regarding "why this is happening". If you need more information about subscribers and observables, I recommend opening a separate question. – Connor Low Apr 13 '21 at 16:49
  • Connor you should close this question as duplicate – Atamyrat Babayev Apr 13 '21 at 16:50
  • Thanks, the other question did help me understand it better, I might ask another question about observables but will make that its own question if I do. Is it best I leave this question up or delete it? – user14216042 Apr 13 '21 at 17:08
  • I don't think it would hurt to leave it. [It can serve as a sign post](https://stackoverflow.com/help/duplicates). – Ivar Apr 13 '21 at 17:11

0 Answers0