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
| Field | Type | Description |
|---|---|---|
data | Ref<Data | null | undefined> | Most recently accepted data |
error | Ref<unknown | null> | Most recently accepted error |
loading | Ref<boolean> | Whether an accepted request is still running |
trigger | (...params: Params) => Promise<Data> | Starts the handler with typed parameters |
useAsync options
| Option | Default | Description |
|---|---|---|
initialData | null | Initial data and the value restored by the operation |
concurrency | "all" | Use "latest" to prevent older requests updating state |
dataOnError | Preserve | Produces replacement data after a rejected request |
isEqual | Fields | Suppresses updates when the previous and next states are equal |
onSuccess | — | Runs after an accepted successful update |
onError | — | Runs 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.
| Control | Description |
|---|---|
fetch() | Starts a request and returns its promise |
abort() | Aborts the current request, when one exists |