Skip to content

Commit

Permalink
logic: provide result of async call
Browse files Browse the repository at this point in the history
  • Loading branch information
dnbrwstr committed Apr 21, 2023
1 parent 2910e4c commit e0fe5a9
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions ui/src/logic/useAsyncCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ import { useCallback, useState } from 'react';

export type Status = 'initial' | 'loading' | 'success' | 'error';

export function useAsyncCall<ReturnValue>(cb: (...args: any[]) => Promise<ReturnValue>) {
export function useAsyncCall<ReturnValue>(
cb: (...args: any[]) => Promise<ReturnValue>
) {
const [status, setStatus] = useState<Status>('initial');
const [result, setResult] = useState<ReturnValue | null>(null);
const [error, setError] = useState<Error | null>(null);

const call = useCallback(
(...args: any[]) => {
setStatus('loading');
cb(...args)
.then((result) => {
setResult(result);
setStatus('success');
return result;
})
Expand All @@ -25,6 +29,7 @@ export function useAsyncCall<ReturnValue>(cb: (...args: any[]) => Promise<Return
return {
call,
status,
error
error,
result,
};
}

0 comments on commit e0fe5a9

Please sign in to comment.