Svelte API

Stores and helpers exported by @omni-async/svelte.

useAction(handler, options)

Creates an on-demand async action using the same state contract as {@link useAsync}.

Example:

const removeUser = useAction((id: string) => api.deleteUser(id));
await removeUser.trigger("42");

useAsync(handler, options)

Creates Svelte stores for an async handler with configurable concurrency and fallback data.

Example:

const save = useAsync((name: string) => api.saveProfile({ name }));
await save.trigger("Ada");

useFetch(handler, options)

Runs an abortable fetch on mount and exposes controls for refetching or cancellation.

Example:

const profile = useFetch((signal) => fetchProfile({ signal }));
await profile.fetch();

useQuery(handler, options)

Creates a latest-request-wins Svelte query for data that may be refreshed on demand.

Example:

const users = useQuery(() => api.listUsers(), { initial: () => [] });
await users.trigger();

Shared result stores

FieldTypeDescription
dataReadable<Data | null | undefined>Most recently accepted data
errorReadable<unknown | null>Most recently accepted error
loadingReadable<boolean>Whether an accepted request is still running
trigger(...params: Params) => Promise<Data>Starts the handler with typed parameters

useAsync options

OptionDefaultDescription
initialDatanullInitial data and the value restored by the operation
concurrency"all"Use "latest" to prevent older requests updating state
dataOnErrorPreserveProduces replacement data after a rejected request
isEqualFieldsSuppresses updates when the previous and next states are equal
onSuccessRuns after an accepted successful update
onErrorRuns after an accepted error update

Active work is aborted when the owning component is destroyed.

useQuery

useQuery uses "latest" concurrency. Its optional initial function supplies initial data and is called again to produce fallback data after an error.

useAction

useAction is an alias of useAsync; it exposes the same stores and returns the resolved value from trigger.

useFetch

useFetch calls fetch() on mount. Starting another fetch aborts the previous controller, and the current controller is aborted when the component is destroyed.

ControlDescription
fetch()Starts a request and returns its promise
abort()Aborts the current request, when one exists