Search code examples
javascriptlodash

Lodash debounce not working


const { debounce } = require('lodash');

debounce(
   () => {
     console.log('testing..');
   },
  1000,
  { leading: true, trailing: false }
);

The above code does not work.
https://lodash.com/docs/4.17.4#debounce All the examples in the docs use named functions.
Is there an issue with using Loash debounce with anonymous function?


Solution

  • Why is the variable name in braces?

    At any rate, lodash's debounce function is a higher order function and will return a debounced function. So you should use it like this.

    const debounce = require('lodash/debounce');
    const debouncedFunction = debounce(() => {
        console.log('debounced')
    }, 1000)
    

    EDIT: Just wanted to note that the braces are for destructuring the require, and are valid syntax. This is good for libraries that don't implement the <library>/<property> as lodash does.