awesome-debounce-promise icon indicating copy to clipboard operation
awesome-debounce-promise copied to clipboard

Add cancel() method

Open tkrotoff opened this issue 6 years ago • 8 comments

Most debounce functions provide a cancel() method.

Use case: https://stackoverflow.com/q/23123138#comment57603349_28046731

  myMethod = () => {
    // ...
  };

  myDebouncedMethod = debounce(myMethod, 200);

  componentWillUnmount() {
    this.myDebouncedMethod.cancel();
  }

tkrotoff avatar Oct 24 '18 16:10 tkrotoff

This would be nice. Just wanted to also stop by and say this library works nicely. I like the whole debounce per key thing most 👍

code-matt avatar Nov 08 '18 23:11 code-matt

thanks :)

will look at providing a cancel method

slorber avatar Nov 09 '18 09:11 slorber

Any update on this? Would also love a cancel method!

daviddelusenet avatar Jan 23 '19 13:01 daviddelusenet

Hey,

I'd like to work on this but I'm not sure what API you have in mind and what usecase.

If it's to handle unmounted react components, you can easily handle that directly inside the promise callback

Otherwise, it's already possible to "cancel" a promise in userland. I have internal code that do so in the lib that I could expose for conveniency: https://github.com/slorber/awesome-debounce-promise/blob/master/index.js#L12

If that is not enough, please show me your usecase + how you would like that cancellation to work?

slorber avatar Jan 23 '19 14:01 slorber

Hi,

I've extracted a new package from this lib to easily add promise cancellation to any existing promise. https://github.com/slorber/awesome-imperative-promise

I've not included any "cancel" method directly in this lib because the feature can be plugged in userland and I think it's better to keep the returned value of the async function to have exactly the same type as the original function

Maybe if this is not enough I could assign a cancel method to the returned function? But it would have to take a "key" arg so that people using the key feature could cancel the right debounced function

slorber avatar Feb 22 '19 11:02 slorber

Will probably try to support this anyway. I've given it a try but didn't find an easy solution. Will try to think about how to implement this soon.

Will probably implement this first in https://github.com/slorber/awesome-imperative-promise and provide the ability to add resolve/reject/cancel methods directly to an async function, instead of adding those methods only to a promise. If anyone want to give a try, contributions are welcome ;)

slorber avatar Feb 26 '19 09:02 slorber

Hey,

I've been playing with request abortion recently. It's not yet supported by this library (I'm using TS and it's quite hard to handle the typing but I see how it could be done)

In the meantime, if you use React and hooks you can try https://github.com/slorber/react-async-hook which support cancellation, you'll just need to pass an abortSignal as parameter to your async call and it will be called as soon as your react component is not interested anymore by the async call result.

slorber avatar May 10 '19 08:05 slorber

Are there any examples anywhere with fetch cancellation now? I've looked but can't find any. This is a very common use case, so was surprised.

braco avatar Jul 13 '21 16:07 braco