Vue API

Composables exported by @omni-async/vue.

useAction(handler, options?)

Creates an on-demand Vue async action without retaining its resolved value.

Example:

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

useAsync(handler, options)

Creates Vue refs 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 when the component mounts and cancels it on unmount.

Example:

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

useQuery(handler, options?)

Creates a latest-request-wins Vue query that may optionally share an external data ref.

Example:

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

Shared result state

FieldTypeDescription
dataRef<Data | null | undefined>Most recently accepted data
errorRef<unknown | null>Most recently accepted error
loadingRef<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

When called inside an active Vue scope, useAsync unsubscribes and aborts active work when that scope is disposed.

useQuery

useQuery uses "latest" concurrency. Pass data to synchronize results with an existing ref, or use initial to create initial and error-fallback data.

useAction

useAction exposes error, loading, and trigger. The resolved value is returned by trigger instead of being retained in a data ref.

useFetch

useFetch calls fetch() on mount and aborts the current controller on unmount.

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