UNPKG

@thibault.sh/hooks

Version:

A comprehensive collection of React hooks for browser storage, UI interactions, and more

1 lines 3.05 kB
{"version":3,"sources":["../src/hooks/useAsync.ts"],"names":["useAsync","asyncFunction","status","setStatus","useState","useCallback","args","__async","result","error"],"mappings":"0FAyCO,SAASA,EAAYC,CAG1B,CAAA,CACA,GAAM,CAACC,CAAAA,CAAQC,CAAS,CAAIC,CAAAA,cAAAA,CAAwB,CAClD,SAAW,CAAA,CAAA,CAAA,CACX,MAAO,IACP,CAAA,KAAA,CAAO,IACT,CAAC,CAAA,CAmBD,OAAO,CAAE,OAAA,CAjBOC,kBACd,CAAUC,GAAAA,CAAAA,GAAgBC,oBAAA,IACxBJ,CAAAA,IAAAA,CAAAA,WAAAA,CAAAA,CAAAA,CAAU,CAAE,SAAW,CAAA,CAAA,CAAA,CAAM,MAAO,IAAM,CAAA,KAAA,CAAO,IAAK,CAAC,CAAA,CACvD,GAAI,CACF,IAAMK,CAAS,CAAA,MAAMP,EAAc,GAAGK,CAAI,EAC1CH,CAAU,CAAA,CAAE,UAAW,CAAO,CAAA,CAAA,KAAA,CAAO,KAAM,KAAOK,CAAAA,CAAO,CAAC,EAC5D,CAAA,MAASC,EAAO,CACdN,CAAAA,CAAU,CACR,SAAW,CAAA,CAAA,CAAA,CACX,MAAOM,CAAiB,YAAA,KAAA,CAAQA,EAAQ,IAAI,KAAA,CAAM,OAAOA,CAAK,CAAC,EAC/D,KAAO,CAAA,IACT,CAAC,EACH,CACF,GACA,CAACR,CAAa,CAChB,CAEkB,CAAA,MAAA,CAAAC,CAAO,CAC3B","file":"useAsync.cjs","sourcesContent":["import { useState, useCallback } from \"react\";\n\ninterface AsyncState<T> {\n isLoading: boolean;\n error: Error | null;\n value: T | null;\n}\n\n/**\n * Hook that manages async operations with loading, error, and success states.\n *\n * Provides a clean interface for handling asynchronous functions with automatic\n * state management for loading indicators and error handling.\n *\n * @template T - The type of data returned by the async function\n * @param asyncFunction - The async function to execute\n *\n * @returns An object containing:\n * - `execute`: Function to trigger the async operation\n * - `status`: Current state with `isLoading`, `error`, and `value` properties\n *\n * @example\n * ```tsx\n * const fetchUser = async (id: string) => {\n * const response = await fetch(`/api/users/${id}`);\n * return response.json();\n * };\n *\n * const { execute, status } = useAsync(fetchUser);\n *\n * // In your component\n * if (status.isLoading) return <div>Loading...</div>;\n * if (status.error) return <div>Error: {status.error.message}</div>;\n * if (status.value) return <div>User: {status.value.name}</div>;\n *\n * // Trigger the async operation\n * <button onClick={() => execute('user-123')}>Load User</button>\n * ```\n *\n * @see https://thibault.sh/hooks/use-async\n */\nexport function useAsync<T>(asyncFunction: (...args: any[]) => Promise<T>): {\n execute: (...args: any[]) => Promise<void>;\n status: AsyncState<T>;\n} {\n const [status, setStatus] = useState<AsyncState<T>>({\n isLoading: false,\n error: null,\n value: null,\n });\n\n const execute = useCallback(\n async (...args: any[]) => {\n setStatus({ isLoading: true, error: null, value: null });\n try {\n const result = await asyncFunction(...args);\n setStatus({ isLoading: false, error: null, value: result });\n } catch (error) {\n setStatus({\n isLoading: false,\n error: error instanceof Error ? error : new Error(String(error)),\n value: null,\n });\n }\n },\n [asyncFunction]\n );\n\n return { execute, status };\n}\n"]}