1

It is simple problem. I am working on angular2+ app and on button click I am executing HTTP Post request but it takes a while to respond. The issue is when the user hits the button multiple times before I got the response. I have figure it out in traditional way. Create a lock variable make sure it is false before executing the function and true while it is executing set it back to false when I got response. But I am wondering if there is RX smart way to do it ? Thanks

2 Answers2

1

Your approach sounds fine. Alternatively, you could debounce the function that is making the HTTP requests. Use lodash debounce for this. I did look to see if there was an RxJs alternative to debounce but did not find one. I tried debounceTime but it wasn't the behaviour I wanted.

danday74
  • 52,471
  • 49
  • 232
  • 283
  • no debounce alternative in rxjs?? what about debounceTime?? https://www.learnrxjs.io/operators/filtering/debouncetime.html – ggradnig Aug 09 '18 at 19:21
  • debounceTime is just setting fixed amount of time between the stream of events. I think even it will delay the first one which is already taking long as I mentioned – Abdelrhman Hussien Aug 09 '18 at 22:12
0

Please share the code. You can use RXJS share operator.

return this.service.pipe(
  map(response=> response),
  share()
);
Suresh Kumar Ariya
  • 9,516
  • 1
  • 18
  • 27