xnimorz/use-debounce

useDebouncedCallback should return the callback function

Closed this issue · 3 comments

Describe the bug
A clear and concise description of what the bug is.

the useDebounce hook should return the function that is passed in, for things like promise chaining

To Reproduce
Steps to reproduce the behavior.

  1. Pass in a Promised function to useDebouncedCallback
  2. Notice that it returns undefined

Expected behavior
A clear and concise description of what you expected to happen.

It should return the Promised function

use-debounce version: 8.0.4

Hey @cgarrovillo

Can you provide an example of this and the use case, please?

const debouncedPromise = useDebounceCallback(() => axios.get('test.com'), 250)

// ...

debouncedPromise().then(axiosResponse => ...).catch(axiosError => ...)

Hey @cgarrovillo
This limitation was made for a reason:
When you plan debounced callback you may not know which callback will be executed in the end. It can lead to errors and unexpected behaviour in the client's application.

To maintain a reliable and sustainable way it's better to move then inside the callback:

const debouncedCallback = useDebounceCallback(() => {
    axios.get('test.com').then(axiosResponse => ...).catch(error => ...)
  }, 250)

// ...

useDebounceCallback();

This approach also works if you have several places with useDebounceCallback