--- title: useAsync description: "`useAsync` is a custom hook that executes an asynchronous function and tracks its state." links: - source: https://github.com/yamada-ui/yamada-ui/tree/main/packages/react/src/hooks/use-async - storybook: https://yamada-ui.github.io/yamada-ui?path=/story/hooks-useasync--basic --- ```tsx const [flg, { toggle }] = useBoolean() const { value, error, loading } = useAsync( async () => new Promise((resolve, reject) => { setTimeout(() => { if (Math.random() > 0.5) { resolve("Succeeded.") } else { reject(new Error("A pseudo random error occurred.")) } }, 3000) }), [flg], ) return ( {loading ? ( Loading... ) : error ? ( Error: {error.message} ) : ( Value: {value} )} ) ``` ## Usage ```tsx import { useAsync } from "@yamada-ui/react" ``` ```tsx import { useAsync } from "@/components/ui" ``` ```tsx import { useAsync } from "@workspaces/ui" ``` ```tsx const { value, error, loading } = useAsync(async () => {}, []) ```