Files
nostrudel/src/hooks/use-async-error-handler.ts
2023-08-24 09:06:13 -05:00

15 lines
453 B
TypeScript

import { useToast } from "@chakra-ui/react";
import { DependencyList, useCallback } from "react";
export default function useAsyncErrorHandler<T = any>(fn: () => Promise<T>, deps: DependencyList = []): () => Promise<T | undefined> {
const toast = useToast();
return useCallback(async () => {
try {
return await fn();
} catch (e) {
if (e instanceof Error) toast({ description: e.message, status: "error" });
}
}, deps);
}