Core API
createAsync(handler, options?)
Creates an AsyncOperation. The handler receives an AsyncContext followed by the parameters passed to execute.
const operation = createAsync(async ({ signal, requestId }, id: string) => loadUser(id, signal), {
concurrency: "latest",
abortable: true,
});
Handler context
| Field | Type | Description |
|---|---|---|
signal | AbortSignal | null | Present when abortable is enabled |
requestId | number | Monotonically increasing operation-local request identifier |
Options
| Option | Default | Description |
|---|---|---|
initialData | null | Data used for the initial and reset snapshots |
dataOnError | Preserve data | Produces replacement data after a rejection |
concurrency | "all" | Selects all-request or latest-request state updates |
abortable | false | Creates an AbortController for every execution |
isEqual | All fields | Determines whether a new snapshot is ignored |
onSuccess | — | Runs after an accepted successful state update |
onError | — | Runs after an accepted error state update |
Operation
| Method | Description |
|---|---|
getSnapshot() | Returns the current frozen snapshot |
subscribe(listener) | Subscribes to accepted snapshot changes and returns an unsubscribe function |
execute(...params) | Starts the handler and returns its promise |
abort() | Aborts controllers, invalidates active work, and returns to idle |
reset() | Invalidates active work and restores initial state |
Error behavior
execute() does not consume errors. It updates the snapshot and invokes onError, then rejects with the original error so the caller decides how to handle it.
try {
await operation.execute();
} catch (error) {
// Handle the same rejection stored in operation.getSnapshot().error.
}