Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 258
GN. Avatar asked Mar 26 '17 19:03

GN.


2 Answers

Yes, it doesn't work, because you don't call it. add () before ; - and it will work

like image 184
Mihey Mik Avatar answered Oct 09 '22 00:10

Mihey Mik


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.

like image 28
Jack Avatar answered Oct 09 '22 00:10

Jack