Typescript rxjs : is there any clean alternative to setimeout 0

Withing my angular app , I’m using this kind of treatment

 setTimeout(() => {

As I was told : setTimeout without a delay (0) seems to wait the next tick where all treatments are done to begin my treament. it’s a planification for the near future.

Since I need this behaviour

Is there any clean aletrnative to do it the same with typescript or rxjs to do it better ?

Suggestions ?

11 thoughts on “Typescript rxjs : is there any clean alternative to setimeout 0”

  1. You can use Interval or take in rxjs;

    import { interval } from 'rxjs';
    import { take} from 'rxjs/operators';
    myMethod() {
       ).subscribe(value =>
  2. This should work, and is simpler than previous solutions:

    timer(0).subscribe(time => this.router.navigate(['mucomponent']));

    timer() takes 2 parameters, the second is optional. If 2 parameters are provided, the first parameter is the delay, and the second parameter is the period. If only one parameter is provided, it is the delay, and the timer will only emit 1 value (it won’t repeat).

    From the documentation for timer():

    If period is not specified (the second parameter), the output Observable emits only one
    value, 0.



Leave a Comment