Skip to main content

Overview

The withAsync extension adds comprehensive async state management to atoms or actions that return promises. It automatically tracks pending operations, manages errors, and provides lifecycle hooks for handling async events. This extension preserves Reatom context across async operations, ensuring that async results properly update Reatom state.

Type Signature

Parameters

AsyncOptions
Configuration options for async handling

Return Value

Returns the target extended with the following properties:
Computed<boolean>
Computed atom that indicates when no async operations are pending
Computed<number>
Computed atom tracking how many async operations are currently pending
Atom<undefined | Error>
Atom containing the most recent error or undefined if no error has occurred
Action<[payload, params], { payload, params }>
Action that is called when the promise resolves successfully
Action<[error, params], { error, params }>
Action that is called when the promise rejects with an error
Action
Action called after either successful resolution or rejection
Action<[], Promise<Payload>>
Action that retries the last async operation
  • For atoms: re-evaluates the computed atom
  • For actions: calls it with the cached params (requires cacheParams: true)
Atom<null | Params>
Atom that caches the last called parameters (requires cacheParams: true)
AsyncStatusAtom
Atom that tracks detailed async operation status (requires status: true)

Examples

Basic Usage with Action

Error Handling

Retry with Computed

Retry with Action (Cached Params)

Custom Error Parsing

With Status Tracking