Home > Net >  Can i set a rxjs timer in a subscribe method in Angular?
Can i set a rxjs timer in a subscribe method in Angular?

Time:10-19

    myfunction$.subscribe(res=>{
this.myfunc2()
this.myfunc3()
this.loading=false // I want to this line run a little bit later

})

How can I achive that adding a delay to the 3.line? If I give a timer in the pipe it will make delay to the all 3 thing or am i wrong? Settimeout would be work may be but I want to do it in reactive way

CodePudding user response:

myfunction$.pipe(
  tap(res => {
    this.myfunc2();
    this.myfunc3();
  }),
  delay(1000), // or whatever time you want
).subscribe(() => this.loading=false);

CodePudding user response:

You want two effects to happen. So I'd do this:

const sharedMyFunction$ = myFunction$.pipe(
  shareReplay(1) // or just share() if my myFunction$ is asynchronous.
);

sharedMyFunction$.subscribe(res=>{
  this.myfunc2()
  this.myfunc3()
})

sharedMyFunction$.pipe(
  take(1),
  delay(1000)
).subscribe(res=> {
  this.loading = false;
})
  • Related