Coder Social home page Coder Social logo

Comments (4)

raon0211 avatar raon0211 commented on August 14, 2024 1

Hi, thanks for your suggestion!

I’ve thought about adding a cancel feature to throttle, but the way throttle works makes it tricky. Since throttle doesn't actually queue any asynchronous tasks, it’s hard to define what exactly would be canceled. That’s why we haven’t included a cancel option in throttle.

What are your thoughts?

from es-toolkit.

Hanna922 avatar Hanna922 commented on August 14, 2024

I simply thought that the cancel feature would cancel the pending function and make it immediately callable by setting lastCallTime to null. For example, it should be able to pass the following test code.

it('should call the function immediately after cancel is called', async () => {
  const func = vi.fn();
  const throttledFunc = throttle(func, 100);

  throttledFunc();
  expect(func).toHaveBeenCalledTimes(1);

  throttledFunc.cancel();
  throttledFunc();
  expect(func).toHaveBeenCalledTimes(2);
});

How do you feel about providing this kind of cancel?

from es-toolkit.

raon0211 avatar raon0211 commented on August 14, 2024

As you mentioned, providing those kinds of cancel options might be helpful.

However, I'm not sure if it is really that beneficial. For debounce, we have to cancel the debounced function when the component is cleaned up, but for throttle, there's no need to do so.

Additionally, since we can only abort AbortControllers once, it might not align well with the definition of cancel for throttle.

So, because it is not a major use case, I think we might not support cancel for throttle in es-toolkit. What do you think?

from es-toolkit.

Hanna922 avatar Hanna922 commented on August 14, 2024

Yes, I agree that it is not a major use case. I'll close this issue. Thank you for opinion 🙇🏻‍♀️

from es-toolkit.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.