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 useDebounceFn return fetchable #2566

Closed
4 tasks done
ppaglier opened this issue Dec 21, 2022 · 0 comments · Fixed by #2580
Closed
4 tasks done

Make useDebounceFn return fetchable #2566

ppaglier opened this issue Dec 21, 2022 · 0 comments · Fixed by #2580
Labels
enhancement New feature or request

Comments

@ppaglier
Copy link

Clear and concise description of the problem

When we make a debouced method with returned values, we're not allowed to get the values, which could be usefull in some cases

const deboucedFn = useDebounceFn(() => 'value', 100);
console.log(deboucedFn()); // undefined

This will currently return undefined

Suggested solution

We can make this method return a promise with the returned values

const deboucedFn = useDebounceFn(() => 'value', 100);
console.log(deboucedFn()); // Promise<'value'>

This can return promise that will be resolve with 'value'

As example for this implementation, i've done in a project this method

function useDeboundeFnWithRet<Args extends unknown[], R>(fn: (...args: Args) => Promise<R>, time: number) {
  let timerId: NodeJS.Timeout;

  return function debounced(...args: Args) {
    if (timerId) {
      clearTimeout(timerId);
    }

    return new Promise<R>((resolve) => {
      timerId = setTimeout(() => resolve(fn(...args)), time);
    });
  };
}

Alternative

No response

Additional context

No response

Validations

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

Successfully merging a pull request may close this issue.

1 participant