Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cancel() method #3

Open
tkrotoff opened this issue Oct 24, 2018 · 8 comments
Open

Add cancel() method #3

tkrotoff opened this issue Oct 24, 2018 · 8 comments

Comments

@tkrotoff
Copy link
Contributor

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
Copy link

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
Copy link
Owner

slorber commented Nov 9, 2018

thanks :)

will look at providing a cancel method

@daviddelusenet
Copy link

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

@slorber
Copy link
Owner

slorber commented Jan 23, 2019

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
Copy link
Owner

slorber commented Feb 22, 2019

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 slorber closed this as completed Feb 22, 2019
@slorber slorber reopened this Feb 26, 2019
@slorber
Copy link
Owner

slorber commented Feb 26, 2019

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
Copy link
Owner

slorber commented May 10, 2019

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
Copy link

braco commented Jul 13, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants