useAsync
Manages the full lifecycle of an async function — idle, pending, fulfilled, and rejected — with built-in abort handling and optional auto-execution.
Breaking change in 1.0
The AbortSignal is now the first argument of fn, followed by the arguments passed to trigger. In 0.x it was appended as the last argument. See the migration guide.
Signature
type AsyncFunction = (signal: AbortSignal, ...args: any[]) => Promise<any>;
function useAsync<F extends AsyncFunction>(
fn: F,
options?: UseAsyncOptions<Awaited<ReturnType<F>>>
): {
trigger: (...args: TriggerArgs<F>) => Promise<void>;
reset: () => void;
status: "idle" | "pending" | "fulfilled" | "rejected";
data: Awaited<ReturnType<F>> | undefined;
error: unknown;
loading: boolean;
};
TriggerArgs<F> is every parameter of fn after the signal. Optional parameters stay optional.
Options
| Property | Type | Default | Description |
|---|---|---|---|
immediate | boolean | false | Run fn automatically on mount. Only for functions that take no arguments besides the signal |
onSuccess | (data: T) => void | — | Called when the promise resolves |
onError | (error: unknown) => void | — | Called when the promise rejects |
Return value
| Property | Type | Description |
|---|---|---|
status | "idle" | "pending" | "fulfilled" | "rejected" | Current lifecycle state |
data | T | undefined | Resolved value, or undefined |
error | unknown | undefined | Rejection reason, or undefined |
loading | boolean | true while status === "pending" |
trigger | (...args) => Promise<void> | Run the async function with the given arguments |
reset | () => void | Abort any in-flight request and reset state back to idle |
Key behaviours
fnreceives anAbortSignalas its first argument. Pass it tofetch(or any abortable API) for automatic cancellation. Functions that don't need it can omit the parameter.- The arguments of
triggerare forwarded after the signal:trigger(id, page)callsfn(signal, id, page). - Re-triggering while a request is in-flight cancels the previous request automatically; only the latest result is stored.
- The in-flight request is aborted on unmount.
fn,onSuccessandonErrorcan be inline functions: the latest version is always used.
Passing arguments
import { useAsync } from "@julianfere/hooked";
function UserCard() {
const { data, loading, trigger } = useAsync(
(signal, id: string, includePosts?: boolean) =>
fetch(`/api/users/${id}?posts=${includePosts ?? false}`, { signal }).then(
(res) => res.json() as Promise<User>
)
);
return (
<>
<button onClick={() => trigger("42")}>Load user</button>
<button onClick={() => trigger("42", true)}>Load user with posts</button>
{loading ? <p>Loading…</p> : <p>{data?.name}</p>}
</>
);
}
When to use React 19 APIs instead
React 19 ships primitives that cover some cases useAsync used to handle. Prefer them when they fit:
| Use case | Recommended |
|---|---|
| Read data during render with Suspense and error boundaries | use(promise) |
| Form submissions and mutations with pending state | useActionState / useTransition |
| Imperative requests triggered by events, with cancellation and callbacks, without Suspense | useAsync |
Live example
Loading playground...