Question:
what is difference betweenactually I want a function to which I can pass many different callbacks, and it will run only the last one,but i dont want to get my apiCallFunk with importing it like the second one. i want my apiCallFunk as argument. because of this I have to use first one.
the use:
Answer:
The code with your first snippet is callingdebounce
three times, creating three different yui
functions that are debounced individually (each of them being a closure with its own let timer
), and then calls each of them once.The code with your second snippet is calling
debounce
once, then calls that debounced function three times, causing the timer mechanism to prevent two of the logs.Actually I want a function to which I can pass many different callbacks, and it will run only the last one, because of this I have to use first one.
No, you need the second approach where
debouce
is only called once and a single timer
is created. What you really want is a debounce
function that passes through arguments:debounce()
call, it creates a module-level state and it will be impossible to create multiple different debounced functions with separate state or timeout lenghts (e.g. for multiple instances of the component). Calling debouce
immediately and exporting the result is the same as writing a moduledebounce
function itself, and then call it in your component where onSearch
is needed:If you have better answer, please add a comment about this, thank you!