Debounce asynchronous functions
Debounce asynchronous functions.
Debouncing means that given function is only run after no calls to it have happened for
x milliseconds. With functions that do asynchronous work, i.e. not finish in the same tick, you also want no calls to happen while the function is currently running - limit the concurrency to one - and rerun with new arguments afterwards.
var debounce = require'async-debounce';console.log'start' num;setTimeoutconsole.log'done' num;done;200;var debounced = debounceasync 50;console.log'call 1'; debounced1;setTimeout console.log'call 2'; debounced2 100;setTimeout console.log'call 3'; debounced3 200;
And in the output you can see that the function is run at max once at a time and if the debounce triggers while the function is still running, it will be queued.
$ node example.jscall 1start 1call 2call 3done 1start 3done 3
Install with npm:
$ npm install async-debounce
Install with component(1):
$ component install juliangruber/async-debounce
Returns a decorated version of
fn that when called calls
fn only when no further calls have happended for
interval milliseconds and if it's not currently running. When it's done running and a call has happened while it was still running, it's called again with latest arguments.