aboutsummaryrefslogtreecommitdiff
path: root/src/utils/react.tsx
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils/react.tsx')
-rw-r--r--src/utils/react.tsx13
1 files changed, 11 insertions, 2 deletions
diff --git a/src/utils/react.tsx b/src/utils/react.tsx
index 69c1df2..a4c7152 100644
--- a/src/utils/react.tsx
+++ b/src/utils/react.tsx
@@ -67,6 +67,7 @@ interface AwaiterOpts<T> {
fallbackValue: T;
deps?: unknown[];
onError?(e: any): void;
+ onSuccess?(value: T): void;
}
/**
* Await a promise
@@ -94,8 +95,16 @@ export function useAwaiter<T>(factory: () => Promise<T>, providedOpts?: AwaiterO
if (!state.pending) setState({ ...state, pending: true });
factory()
- .then(value => isAlive && setState({ value, error: null, pending: false }))
- .catch(error => isAlive && (setState({ value: null, error, pending: false }), opts.onError?.(error)));
+ .then(value => {
+ if (!isAlive) return;
+ setState({ value, error: null, pending: false });
+ opts.onSuccess?.(value);
+ })
+ .catch(error => {
+ if (!isAlive) return;
+ setState({ value: null, error, pending: false });
+ opts.onError?.(error);
+ });
return () => void (isAlive = false);
}, opts.deps);