Function Signature
Parameters
() => Promise<T>
required
An async function that returns a Promise. This function is called immediately when the atom is created and whenever the atom is recomputed (if it depends on other atoms).
AtomOptions<AsyncAtomState<T>>
Optional configuration for the atom.
Return Type
ReadonlyAtom<AsyncAtomState<T>>
Returns a readonly atom containing the async operation state. The state is a discriminated union:
Examples
Basic Async Atom
Handling All States
Subscribing to Async State Changes
Reactive Async Atoms
Error Handling
TypeScript Error Types
Combining Multiple Async Atoms
Retry Logic
Key Features
Automatic State Tracking
Tracks pending, success, and error states automatically without manual state management.
Type-Safe States
TypeScript discriminated unions ensure type safety when accessing data or error properties.
Reactive Dependencies
Automatically refetches when dependent atoms change, keeping async data fresh.
Observable Updates
Subscribers are notified when the async operation completes or fails.
Related APIs
- createAtom - Base atom primitive
- createStore - Higher-level store abstraction
- flush - Manually flush pending updates