debounce-promise
Create a debounced version of a promise returning function
Install
npm i -S debounce-promise
Usage example
var debounce = { return Promise} var saveCycles = ; 1 2 3 4 // Will only call expensiveOperation once with argument `4` and print://=> call no #4//=> call no #4//=> call no #4//=> call no #4
With leading=true
var debounce = { return Promise} var saveCycles = ; 1 2 3 4 //=> call no #1//=> call no #4//=> call no #4//=> call no #4
With accumulate=true
var debounce = { return PromiseallargTuples} var square = ; 1 2 3 4 //=> 1//=> 4//=> 9//=> 16
Api
debounce(func, [wait=0], [{leading: true|false, accumulate: true|false})
Returns a debounced version of func
that delays invoking until after wait
milliseconds.
Set leading: true
if you
want to call func
and return its promise immediately.
Set accumulate: true
if you want the debounced function to be called with an array of all the arguments received while waiting.
Supports passing a function as the wait
parameter, which provides a way to lazily or dynamically define a wait timeout.
Example timeline illustration
{ return }const debounced =
time (ms) -> 0 --- 10 --- 50 --- 100 ---
-----------------------------------------------
debounced() | --- P(1) --- P(1) --- P(1) ---
refresh() | --------------------- P(1) ---
const debounced =
time (ms) -> 0 --- 10 --- 50 --- 100 --- 110 ---
--------------------------------------------------------
debounced() | --- P(1) --- P(2) --- P(2) --- P(2) ---
refresh() | --- P(1) --------------------- P(2) ---