Skip to content

Commit 2408bfa

Browse files
committed
[FIX] isAsync utils and useThrottle hooks
1 parent c714a80 commit 2408bfa

2 files changed

Lines changed: 6 additions & 6 deletions

File tree

packages/react-tools/src/hooks/useThrottle.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ export const useThrottle = <T extends unknown[]>(fn: (...args: T) => void | Prom
2323
fn(...args);
2424
idRef.current = setTimeout(() => pending.current = false, optsRef.current.delay);
2525
} else if (optsRef.current.waitFn) {
26-
if (isAsync(fn)) {
27-
(fn(...args) as Promise<ReturnType<typeof fn>>).finally(() => pending.current = false);
26+
const current = fn(...args);
27+
if (isAsync(current)) {
28+
(current as Promise<void>).finally(() => pending.current = false);
2829
} else {
29-
fn(...args);
3030
pending.current = false;
3131
}
3232
} else {
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/**
22
* **`isAsync`**: It detects if a function is asynchronous.
3-
* @param {(...args: unknown[])=> Eìunknown | Promise<unknown>}
3+
* @param {(...args: unknown[])=> unknown | Promise<unknown>}
44
* @returns {boolean} result
55
*/
6-
export const isAsync = <T extends unknown[], E = unknown>(fn: (...args: T)=> E | Promise<E>): boolean => {
7-
return fn.constructor.name === "AsyncFunction" || (typeof fn === "function" && typeof fn === "object" && typeof (fn as PromiseLike<E>).then === "function");
6+
export const isAsync = <T extends unknown[], E = unknown>(fn: E | Promise<E> | ((...args: T)=> E | Promise<E>)): boolean => {
7+
return (typeof fn === "function" && fn.constructor.name === "AsyncFunction") || (typeof fn === "object" && typeof (fn as PromiseLike<E>).then === "function");
88
}

0 commit comments

Comments
 (0)