throttle.ts

view source

Declarations
#

2 declarations

throttle
#

throttle.ts view source

<T extends (...args: Array<any>) => Promise<void>>(cb: T, { delay, when }?: ThrottleOptions): T import {throttle} from '@fuzdev/fuz_util/throttle.js';

Throttles calls to a callback that returns a void promise. Immediately invokes the callback on the first call unless leading=false. If the throttled function is called while the promise is already pending, the call is queued to run after the pending promise completes plus delay, and only the last call is invoked. In other words, all calls and their args are discarded during the pending window except for the most recent. Unlike debouncing, this calls the throttled callback both on the leading and trailing edges of the delay window by default, and this can be customized by setting when to 'leading' or 'trailing'. It also differs from a queue where every call to the throttled callback eventually runs.

cb

type T

__1

default EMPTY_OBJECT

returns

T

same as cb

generics

throttle<T extends (...args: Array<any>) => Promise<void>>
T
constraint (...args: Array<any>) => Promise<void>

ThrottleOptions
#

throttle.ts view source

ThrottleOptions import type {ThrottleOptions} from '@fuzdev/fuz_util/throttle.js';

delay?

Enforced milliseconds between calls. For when='trailing' this is the debounce delay.

type number

when?

When to call the throttled function. Defaults to both.

type 'both' | 'leading' | 'trailing'

Depends on
#