Skip to content

Commit 84f51bf

Browse files
committed
fix(useDebounce, useThrottle): resolve instead of reject on cancel
Calling cancel() previously rejected the pending promise with "Cancelled", which caused uncaught promise errors when callers didn't attach a .catch() handler. Since cancellation is an expected control flow, resolve with undefined instead. Also applies the same fix to useThrottle which had the identical issue. Closes #365
1 parent a12cd68 commit 84f51bf

3 files changed

Lines changed: 8 additions & 8 deletions

File tree

packages/runed/src/lib/utilities/use-debounce/use-debounce.svelte.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export function useDebounce<Args extends unknown[], Return>(
9494
}
9595

9696
clearTimeout(context.timeout);
97-
context.reject("Cancelled");
97+
context.resolve(undefined as unknown as Return);
9898
context = null;
9999
};
100100

packages/runed/src/lib/utilities/use-debounce/use-debounce.test.svelte.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe("useDebounce", () => {
2020
const debounced = useDebounce(fn, 100);
2121

2222
expect(fn).not.toHaveBeenCalled();
23-
debounced().catch(() => {});
23+
debounced();
2424
expect(fn).not.toHaveBeenCalled();
2525
expect(debounced.pending).toBe(true);
2626
debounced.cancel();
@@ -35,7 +35,7 @@ describe("useDebounce", () => {
3535
const debounced = useDebounce(fn, 100);
3636

3737
expect(fn).not.toHaveBeenCalled();
38-
debounced().catch(() => {});
38+
debounced();
3939
expect(fn).not.toHaveBeenCalled();
4040
expect(debounced.pending).toBe(true);
4141
debounced.cancel();
@@ -44,11 +44,11 @@ describe("useDebounce", () => {
4444
expect(fn).not.toHaveBeenCalled();
4545

4646
// New test
47-
let wasCatchCalled = false;
48-
debounced().catch(() => (wasCatchCalled = true));
49-
expect(wasCatchCalled).toBe(false);
47+
let wasResolved = false;
48+
debounced().then(() => (wasResolved = true));
49+
expect(wasResolved).toBe(false);
5050
await new Promise((resolve) => setTimeout(resolve, 110));
51-
expect(wasCatchCalled).toBe(false);
51+
expect(wasResolved).toBe(true);
5252
expect(fn).toHaveBeenCalledTimes(1);
5353
});
5454

packages/runed/src/lib/utilities/use-throttle/use-throttle.svelte.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export function useThrottle<Args extends unknown[], Return>(
8585
}
8686

8787
clearTimeout(timeout);
88-
reject?.("Cancelled");
88+
resolve?.(undefined as unknown as Return);
8989
reset();
9090
}
9191
};

0 commit comments

Comments
 (0)