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

Make debounce work with asynchronous function #6351

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Antman261
Copy link

Motivation

The async package provides helper functions for asynchronous operations, yet the debounce function does not work with asynchronous functions. Since these functions are commonly the most valuable functions to debounce, It makes sense to design debounce to support asynchrony.

This also allows us to return a value from debounce.

Solution

  • Simplified the types for debuts: it's now able to infer the arguments and return type of the wrapped function.
  • Every function call saves a resolver in an array: Every call to a debounced function will resolve the same result at the same time.

Caveat: I wrote this in the GitHub editor quickly before bed, it probably has a type error I missed somewhere -- will fix it tomorrow, but worth a general review in the meantime so I can include the feedback when I look at it tomorrow evening (Australian timezone).

## Motivation

The async package provides helper functions for asynchronous operations, yet the debounce function does not work with asynchronous functions. Since these functions are commonly the most valuable functions to debounce, It makes sense to design debounce to support asynchrony.

This also allows us to return a value from debounce.

## Solution

* Simplified the types for debuts: it's now able to infer the arguments and return type of the wrapped function.
* Every function call saves a resolver in an array: Every call to a debounced function will resolve the same result at the same time.

Caveat: I wrote this in the GitHub editor before bed, it probably has a type error I forgot about somewhere -- will fix it tomorrow, but worth a review in the meantime.
@Antman261 Antman261 requested a review from kt3k as a code owner January 19, 2025 14:49
@CLAassistant
Copy link

CLAassistant commented Jan 19, 2025

CLA assistant check
All committers have signed the CLA.

@github-actions github-actions bot added the async label Jan 19, 2025
Quickly adding in the resolver type that I missed
@Antman261
Copy link
Author

Woke up this morning and decided to approach asynchronously debouncing in a separate function -- less of a breaking change. I would still like to improve the type definition of the original function, but it may still be too breaking for very little ROI? Happy to roll that one back if you like.

I'll write tests for debounceAsync tonight

@kt3k
Copy link
Member

kt3k commented Jan 20, 2025

yet the debounce function does not work with asynchronous functions.

I don't understand this part well. The current debounce function allow debouncing async functions (You can assign (...) => Promise<X> to (...) => void)

debounce(() => fetch("https://example.com"), 1000);

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

Successfully merging this pull request may close these issues.

3 participants