toss / es-toolkit

A modern JavaScript utility library that's 2-3 times faster and up to 97% smaller—a major upgrade to lodash.
https://es-toolkit.slash.page
Other
6.28k stars 269 forks source link

feat(throttle): add promise based feature #414

Closed dayongkr closed 3 weeks ago

dayongkr commented 4 weeks ago

Description

At now, es-toolkit/throttle has only a time based feature.

But if we also support promise based feature, we can do this:

const throttledFetch = throttle(fetch);

throttledFetch('https://es.toolkit/api/login') // Start the first fetching 
throttledFetch('https://es.toolkit/api/login') // Ignored
// End the first fetching
throttledFetch('https://es.toolkit/api/login') // Start the second fetching
// End the second fetching

Currently, if the throttleMs parameter is not provided, then It works promise based.

I think that this feature is also good for preventing double(or nth)-clicked.

I'm not sure this feature is good for es-toolkit, so I stop working. But this approach is acceptable, then I am going to improve interface and change docs.

vercel[bot] commented 4 weeks ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
es-toolkit ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 22, 2024 4:25am
codecov-commenter commented 4 weeks ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 99.77%. Comparing base (5c551b8) to head (db336da).

Additional details and impacted files [![Impacted file tree graph](https://app.codecov.io/gh/toss/es-toolkit/pull/414/graphs/tree.svg?width=650&height=150&src=pr&token=8N5S3AR3C7&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=toss)](https://app.codecov.io/gh/toss/es-toolkit/pull/414?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=toss) ```diff @@ Coverage Diff @@ ## main #414 +/- ## ======================================= Coverage 99.77% 99.77% ======================================= Files 161 161 Lines 1323 1330 +7 Branches 358 361 +3 ======================================= + Hits 1320 1327 +7 Misses 2 2 Partials 1 1 ```
raon0211 commented 4 weeks ago

I am not really sure if we add this feature to throttle. However, we might add this called throttleAsync as an async library.

dayongkr commented 3 weeks ago

I am not really sure if we add this feature to throttle. However, we might add this called throttleAsync as an async library.

I agree with adding a method called throttleAsync. I will reopen the issue for further discussion.