import { Readable, readable, derived } from "svelte/store"; interface AsyncData { value: Readable; error: Readable; pending: Readable; success: Readable; } function useAsync(asyncFunction: () => Promise): AsyncData { const promise = asyncFunction(); const value = readable(null, (set: (value: T) => void) => { promise.then((value: T) => set(value)); }); const error = readable(null, (set: (value: E) => void) => { promise.catch((value: E) => set(value)); }); const pending = readable(true, (set: (value: boolean) => void) => { promise.finally(() => set(false)); }); const success = derived([value], (_, set) => set(true), false); return { value, error, pending, success }; } export default useAsync;