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

FieldTypeDescription
signalAbortSignal | nullPresent when abortable is enabled
requestIdnumberMonotonically increasing operation-local request identifier

Options

OptionDefaultDescription
initialDatanullData used for the initial and reset snapshots
dataOnErrorPreserve dataProduces replacement data after a rejection
concurrency"all"Selects all-request or latest-request state updates
abortablefalseCreates an AbortController for every execution
isEqualAll fieldsDetermines whether a new snapshot is ignored
onSuccessRuns after an accepted successful state update
onErrorRuns after an accepted error state update

Operation

MethodDescription
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.
}