a throttled function blocks the execution of fn, if less than delta_time_ms amount of time has passed since the previous non-rejected call.
a rejected caller would receive the THROTTLE_REJECT symbol, instead of the fn function's evaluated value.
Visualization
the following visual illustration should give you an idea of how a throttled function with delta_time_ms = 1000 would behave.
the intersections in the call made axis specifies the time where a call is made to the throttled function.
successfully evaluated calls are labeled as resolved (with the legend marker o), and their caller will receive the evaluated value of fn()
unsuccessful calls (i.e. rejected) are labeled as rejected (with the legend marker x), and their caller will receive the symbol value THROTTLE_REJECT.
│resolved │ o o o
│rejected │ │ x x x │ x │ x x
│call made├──┴─┴──┴─┴──┴─┴────────────────┴─┴─┴─────────────────► time
│time │ 0 1 2 3 4 5
// a function that creates an asynchronous delay of the given number of milliseconds constsleep = (time_ms: number) => (newPromise((resolve) => (setTimeout(resolve, time_ms))))
// the function that we plan to apply throttling to constfn = (v: number) => { log_history.push([current_time(), v]) returnv + 100 }
// the throttled version of `fn`, that blocks subsequent calls when they are made under the `delta_time` interval (1000ms here). constthrottled_fn = throttle(1000, fn)
// first call to the `throttled_fn` will be evaluated successfully and immediately consta = throttled_fn(24)
// subsequent calls to the `throttled_fn` that are made under 1000ms, will be immediately rejected with the symbol value `THROTTLE_REJECT`. awaitsleep(200) constb1 = throttled_fn(37) constb2 = throttled_fn(42)
a function (that takes arguments intended for fn) that returns the value of fn if it was not throttled,
otherwise a THROTTLE_REJECT symbol is returned.
a throttled function blocks the execution of
fn
, if less thandelta_time_ms
amount of time has passed since the previous non-rejected call. a rejected caller would receive the THROTTLE_REJECT symbol, instead of thefn
function's evaluated value.Visualization
the following visual illustration should give you an idea of how a throttled function with
delta_time_ms = 1000
would behave.call made
axis specifies the time where a call is made to the throttled function.resolved
(with the legend markero
), and their caller will receive the evaluated value offn()
rejected
(with the legend markerx
), and their caller will receive the symbol value THROTTLE_REJECT.Example