michal-lipski
michal-lipski

Reputation: 91

Can I cancel the RxJs delay?

I have a delay between some backend requests configured to 60 seconds using RxJs delayWhen. What I want to achieve is the possibility to cancel the ongoing delay at some point.

Let's say 30 seconds of delay passed and user is making some action. I want to trigger a backend request immediately, without waiting for the next 30 seconds to pass.

Is there a way to do this using rxJs?

Upvotes: 0

Views: 779

Answers (1)

saglamcem
saglamcem

Reputation: 687

This is theoretical and there might be better ways to do it, but the takeUntil operator might be helpful.

What could happen is that you could have a button on your page. The button, when clicked, could emit an event buttonClicked$. The operation that you want to trigger may listen to that event:

yourDesiredEvent$.pipe(
  takeUntil(buttonClicked$), 
  ...
)

And once your button is clicked (which would emit the buttonClicked$ event), it could cancel listening to the 30 seconds rule and just trigger the backend call.

If any information here is faulty let me know, so I remove it in the hopes to not be misleading. :)

Upvotes: 1

Related Questions