slorber / awesome-debounce-promise

Debounce your API calls easily and stay in promised land.
https://sebastienlorber.com
391 stars 9 forks source link

Add cancel() method #3

Open tkrotoff opened 6 years ago

tkrotoff commented 6 years ago

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();
  }
code-matt commented 6 years ago

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 👍

slorber commented 6 years ago

thanks :)

will look at providing a cancel method

daviddelusenet commented 5 years ago

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

slorber commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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.

braco commented 3 years ago

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.