From 2e92fb1790eae15365025e28647ce24364c199ae Mon Sep 17 00:00:00 2001 From: MatthewPattell Date: Wed, 22 Apr 2026 16:23:44 -0700 Subject: [PATCH] fix(make-fetching): preserve this binding and harden against sync errors - Use callback.apply(instance, args) to preserve this binding when the wrapped method is a class method that references this. Without it, class actions like public async verify() lose context and crash silently. - Wrap callback invocation in try/catch so a sync error before the promise is returned still decrements inFlight instead of leaving the loading state stuck at true. - Guard the thenable check against null (typeof null === object) so callbacks that return null or void do not crash the wrapper. --- src/make-fetching.ts | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/make-fetching.ts b/src/make-fetching.ts index c0e453d..b33889b 100644 --- a/src/make-fetching.ts +++ b/src/make-fetching.ts @@ -73,11 +73,21 @@ function makeFetching>( increment(); - // eslint-disable-next-line @typescript-eslint/no-unsafe-argument - const result = callback(...args); + let result: unknown; - if (typeof result === 'object' && result.finally) { - result.finally(() => { + try { + result = callback.apply(instance, args); + } catch (error) { + decrement(); + throw error; + } + + if ( + result && + typeof result === 'object' && + typeof (result as { finally?: unknown }).finally === 'function' + ) { + void (result as Promise).finally(() => { decrement(); }); } else {