Skip to content

Commit 2baf7d9

Browse files
committed
Document dynamic timeframe with an example
1 parent f36b9ea commit 2baf7d9

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

async/unstable_throttle.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,26 @@ export interface ThrottledFunction<T extends Array<unknown>> {
7070
* assert(func.lastExecution > 0);
7171
* ```
7272
*
73+
* @example With dynamic timeframe
74+
*
75+
* ```ts no-assert
76+
* import { throttle } from "@std/async/unstable-throttle";
77+
*
78+
* function processUserInput(input: string) {
79+
* // Do some expensive computation with user input that changes on each
80+
* // keypress, which takes a variable amount of time depending on the length
81+
* // or complexity of input.
82+
* }
83+
*
84+
* const processUserInputThrottled = throttle(
85+
* processUserInput,
86+
* // Throttle dynamically, waiting twice as long as the previous execution
87+
* // took to complete before starting the next call.
88+
* (n) => n * 2,
89+
* { ensureLastCall: true },
90+
* );
91+
* ```
92+
*
7393
* @typeParam T The arguments of the provided function.
7494
* @param fn The function to throttle.
7595
* @param timeframe The timeframe in milliseconds in which the function should be called at most once.

0 commit comments

Comments
 (0)