@thibault.sh/hooks
Version:
A comprehensive collection of React hooks for browser storage, UI interactions, and more
1 lines • 2.1 kB
Source Map (JSON)
{"version":3,"sources":["../src/hooks/useAsync.ts"],"names":["useAsync","asyncFunction","status","setStatus","useState","useCallback","args","__async","result","error"],"mappings":"qFAaO,SAASA,EAAYC,CAG1B,CAAA,CACA,GAAM,CAACC,CAAAA,CAAQC,CAAS,CAAIC,CAAAA,QAAAA,CAAwB,CAClD,SAAW,CAAA,CAAA,CAAA,CACX,MAAO,IACP,CAAA,KAAA,CAAO,IACT,CAAC,CAAA,CAmBD,OAAO,CAAE,OAAA,CAjBOC,YACd,CAAUC,GAAAA,CAAAA,GAAgBC,IAAA,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.mjs","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 handles async operations with loading and error states\n * @param asyncFunction - Async function to execute\n * @returns Object containing execute function, loading state, error, and value\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"]}