Question:what is difference between
actually 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.
Answer:The code with your first snippet is calling
debouncethree times, creating three different
yuifunctions 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
debounceonce, 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
debouceis only called once and a single
timeris created. What you really want is a
debouncefunction 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
debouceimmediately and exporting the result is the same as writing a module
debouncefunction itself, and then call it in your component where
If you have better answer, please add a comment about this, thank you!