You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tailchat/shared/hooks/useAsyncRequest.ts

20 lines
511 B
TypeScript

import type { DependencyList } from 'react';
import { showErrorToasts } from '../manager/ui';
import type { FunctionReturningPromise } from '../types';
import { useAsyncFn } from './useAsyncFn';
export function useAsyncRequest<T extends FunctionReturningPromise>(
fn: T,
deps: DependencyList = []
) {
const [{ loading }, call] = useAsyncFn(async (...args) => {
try {
await fn(...args);
} catch (err) {
showErrorToasts(err);
}
}, deps);
return [{ loading }, call] as const;
}