the time in milliseconds after which a trailing (pending) call to the function gets resolved if no other calls are made during that time interval.
you would definitely want this to be some value greater than delta_time_ms, otherwise it will be weird because if this value is smaller,
then trailing_time_ms
will become the "effective" throttling time interval, but also one that always resolved later rather than immediately.
the time interval in milliseconds for throttling
the function to be throttled
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 throttled function is made within the trailing_time_ms waiting period. if no rejection value is provided, then the promise will linger around unsettled forever. do note that having a rejection value means that you will have to catch any rejections if you wait for the promise, otherwise it will bubble to the top level as an unhandled error.
a function (that takes arguments intended for fn
) that returns a Promise
to the value of fn
if it is resolved (i.e. not throttled or when trailing),
otherwise if throttled, then that promise will either be never be resolved, or rejected based on if a rejection_value was provided.
a throttle function, similar to throttle, that also insures that the final call (aka trailing call) made to the throttled function always resolves eventually.
this is useful in cases where it is of utmost importance that the throttled function is called one last time with before a prolonged delay.
Visualization
the following visual illustration shows the difference between the regular throttle, and throttleAndTrail functions:
this throttleAndTrail function
here is a function
fn
throttled withtrailing_time_ms = 1500
, anddelta_time_ms = 1000
. as you can see below, the trailing calls to the throttled function do get resolved eventually (1500ms after the last call).regular throttle function
here is a function
fn
throttled withdelta_time_ms = 1000
. as it can be seen below, the final call to the throttled function gets rejected, because it was called too quickly.Example