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 | ComputedRef<Data | null | undefined> | Most recently accepted data |
error | ComputedRef<unknown | null> | Most recently accepted error |
loading | ComputedRef<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 invalidates active work when that scope is disposed.
useQuery
useQuery uses "latest" concurrency. Pass data to use an existing writable
ref, or use initial to create internally owned initial data. A rejected request preserves the
current data and updates error.
When data is provided, result.data is the exact same writable ref and the query's data storage.
Manual edits are immediately visible. An accepted successful request replaces the ref value and may
overwrite those edits.
const query = useQuery(loadUsers, {
data: shallowRef<User[]>([]),
});
query.data.value.push(newUser);
await query.trigger(); // A successful result replaces query.data.value.
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 |