the time interval in milliseconds for debouncing
the function to be debounced
Optional
rejection_value: REJif a rejection value is provided, then old unresolved pending promises will be rejected with the given value, when a new call to the debounced function is made (within the debouncing waiting period)
a function (that takes arguments intended for fn
) that returns a promise, which is resolved once wait_time_ms
amount of time has passed with no further calls
creates a debounced version of the provided function that returns a new promise.
the debounced function delays the execution of the provided function
fn
until the debouncing intervalwait_time_ms
amount of time has passed without any subsequent calls.if a
rejection_value
is provided, then any subsequent calls to the debounced function that are made within the debouncing interval, will reject the previous promises. thus you will have tocatch
them in that case. (otherwise it will result in an error)you may worry that too many calls to a non-rejectable debounced function (i.e. when
rejection_value === undefined
) will create too many promise objects, possibly resulting in memory leaks. however, luckily, modern javascript engines are not afflicted by too many pending promise objects. in fact, choosing to reject promises (i.e. by settingrejection_value
), might be more expensive down the line, as error catching is typically expensive.also check out debounceAndShare, which avoids this "lots of promise objects" issue by sharing the same promise across all quick callers of the debounce. but it will require careful usage, as all promised callers will eventually get resolved, which may create an unintended avalaunch of subsequent
then
calls if not used carefully.Example