import { Signal, useSignal } from "@preact/signals"; import { useEffect } from "preact/hooks"; export type QueryStatus = { type: "loading"; } | { type: "complete"; data: T; } | { type: "error"; err: Error; }; export function useAsync(fn: () => Promise): Signal> { const state = useSignal({ type: "loading", } as QueryStatus); useEffect(() => { (async () => { try { const data = await fn(); state.value = { type: "complete", data: data, }; } catch (err) { state.value = { type: "error", err: err, }; } })(); }, []); return state; }