Skip to content

Function: runOptimisticAction()

Function: runOptimisticAction()

runOptimisticAction<TArgs, TResult, TSnapshot>(args): Promise<TResult>

Defined in: auth-client/src/framework-adapters/shared/action-state.ts:104

Optimistic-update wrapper around runAction. Lets a caller flip the UI to its hoped-for state immediately, run the actual operation, and commit / rollback based on success / failure.

Example (Preact, removing a member from a list):

const remove = useRemoveOrgMember(); const onRemove = (id) => { setMembers((prev) => prev.filter((m) => m.id !== id)); // optimistic remove.run({ orgId, userId: id }).catch(() => { setMembers(snapshot); // rollback on failure }); };

For a hook-level helper, framework adapters can wrap this with their state primitive. The function itself stays framework-agnostic.

Why split this from runAction: not every action is optimistic-safe (a login can’t be “optimistically successful” — you don’t have the tokens yet). Keeping the helper separate makes the choice explicit at the call site.

Type Parameters

TArgs

TArgs extends unknown[]

TResult

TResult

TSnapshot

TSnapshot

Parameters

args

apply

() => void

Apply the optimistic UI change. Called synchronously before op runs.

callArgs

TArgs

op

(…a) => Promise<TResult>

rollback

(snapshot) => void

Roll back to the captured snapshot if op rejects.

setState

(next) => void

snapshot

() => TSnapshot

Capture state before the optimistic flip. Returned for rollback.

Returns

Promise<TResult>