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
| Field | Type | Description |
|---|---|---|
data | Readable<Data | null | undefined> | Most recently accepted data |
error | Readable<unknown | null> | Most recently accepted error |
loading | Readable<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 |
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.
| Control | Description |
|---|---|
fetch() | Starts a request and returns its promise |
abort() | Aborts the current request, when one exists |