proebuck (60) [Avatar] Offline
#1
MEAP v04

pg 132: (ch 3.2.3) listing 3.2


Why bother with subtimeoutId at all?
Why not just use timeoutId for both as only one timeout will exist at a time (as below)?


const progressBar$ = Rx.Observable.create(observer => {
  const INIT_DELAY = 3000;
  const RATE = 50;
  const MAX_VAL = 100;

  let val = 0;
  let timeoutId = 0;

  function progress() {
    if (++val <= MAX_VAL) {
      observer.next(val);
      timeoutId = setTimeout(progress, RATE);
    }
    else {
      observer.complete();
    }
  };

  timeoutId = setTimeout(progress, INIT_DELAY);

  return () => {
    clearTimeout(timeoutId);
  };
}


331872 (130) [Avatar] Offline
#2
Yes, I think that would work and make it cleaner.

Thanks!