Skip to main content

Async Data

Async Data (@ekz/async-data) models the lifecycle of asynchronous values — loading, success, failure, and stale cached data — in a type-safe container inspired by functional patterns.

It pairs well with @ekz/option for optional values and is used throughout the @ekz libraries (for example in @ekz/api).

Why use it?

  • Explicit states instead of ad-hoc loading / error / data booleans
  • Stale-while-revalidate via PendingStale and FailedStale
  • match API for exhaustive rendering
  • observePromise to wire promises into React state safely

States

StateMeaning
EmptyNo value yet
PendingLoading, no previous value
ReadySuccess with a value
PendingStaleReloading, showing previous value
FailedError, no previous value
FailedStaleError after a previous value was loaded

Install

npm install @ekz/async-data @ekz/option
# or
yarn add @ekz/async-data @ekz/option

Peer dependency: @ekz/option ^2.

Next steps