Skip to content

Commit 6415ade

Browse files
committed
Always return void
1 parent 992f519 commit 6415ade

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

async/unstable_throttle.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ export type ThrottleOptions = {
1515
* A throttled function that will be executed at most once during the
1616
* specified `timeframe` in milliseconds.
1717
*/
18-
export interface ThrottledFunction<T extends Array<unknown>, R = void> {
19-
(...args: T): R extends Promise<unknown> ? Promise<void> : void;
18+
export interface ThrottledFunction<T extends Array<unknown>> {
19+
(...args: T): void;
2020
/**
2121
* Clears the throttling state.
2222
* {@linkcode ThrottledFunction.lastExecution} will be reset to `-Infinity` and
@@ -26,7 +26,7 @@ export interface ThrottledFunction<T extends Array<unknown>, R = void> {
2626
/**
2727
* Execute the last throttled call (if any) and clears the throttling state.
2828
*/
29-
flush(): R extends Promise<unknown> ? Promise<void> : void;
29+
flush(): void;
3030
/**
3131
* Returns a boolean indicating whether the function is currently being throttled.
3232
*/
@@ -101,11 +101,11 @@ export interface ThrottledFunction<T extends Array<unknown>, R = void> {
101101
* @returns The throttled function.
102102
*/
103103
// deno-lint-ignore no-explicit-any
104-
export function throttle<T extends Array<any>, R = void>(
105-
fn: (this: ThrottledFunction<T, R>, ...args: T) => R,
104+
export function throttle<T extends Array<any>>(
105+
fn: (this: ThrottledFunction<T>, ...args: T) => void,
106106
timeframe: number | ((previousExecution: number) => number),
107107
options?: ThrottleOptions,
108-
): ThrottledFunction<T, R> {
108+
): ThrottledFunction<T> {
109109
const ensureLast = Boolean(options?.ensureLastCall);
110110
let timeout = -1;
111111

@@ -124,21 +124,19 @@ export function throttle<T extends Array<any>, R = void>(
124124
} finally {
125125
lastExecution = Date.now();
126126
if (isPromiseLike(result)) {
127-
result = result.finally(() => {
127+
result.finally(() => {
128128
lastExecution = Date.now();
129129
if (typeof timeframe === "function") {
130130
tf = timeframe(lastExecution - start);
131131
}
132132
});
133133
} else {
134-
// lastExecution = Date.now();
135134
if (typeof timeframe === "function") {
136135
tf = timeframe(lastExecution - start);
137136
}
138137
}
139138
flush = null;
140139
}
141-
if (isPromiseLike(result)) return result.then(() => {});
142140
};
143141
if (throttled.throttling) {
144142
if (ensureLast) {
@@ -147,15 +145,15 @@ export function throttle<T extends Array<any>, R = void>(
147145
}
148146
return;
149147
}
150-
return flush?.();
151-
}) as ThrottledFunction<T, R>;
148+
flush?.();
149+
}) as ThrottledFunction<T>;
152150

153151
throttled.clear = () => {
154152
lastExecution = -Infinity;
155153
};
156154

157155
throttled.flush = () => {
158-
return flush?.() as ReturnType<ThrottledFunction<T, R>["flush"]>;
156+
flush?.();
159157
};
160158

161159
Object.defineProperties(throttled, {

0 commit comments

Comments
 (0)