Skip to content

Async

Types

AsyncState type

luau
type AsyncState = "unresolved" | "pending" | "ready" | "refreshing" | "errored"

The state of an async node, derived from its .error/.loading and whether a fetch has resolved.

  • unresolved (nothing fetched yet)
  • pending (first fetch in flight, no value)
  • refreshing (fetch in flight over an existing value)
  • ready (resolved)
  • errored (last fetch threw)

Async type

luau
type Async<T> = {
	data: Graph.Node<T?>,
	error: Graph.Node<string?>,
	loading: Graph.Node<boolean>,
	state: Graph.Node<AsyncState>,
	Destroy: (self: Async<T>) -> (),
	refetch: (self: Async<T>, info: any?) -> (),
	mutate: (self: Async<T>, value: T) -> (),
	_resolved: Graph.Node<boolean>,
	_effect: Graph.Node<any>,
	_src: any,
	_fetcher: any,
	_fetchId: number,
}

Functions

new

luau
Async.new<S, T>(source: any, fetcher: ((S?, T, any) -> T) | T?, initialValue: T?): Async<T>

Creates a non-blocking asynchronous node that runs yielding work off the reactive graph and exposes its progress as four nodes: .data, .error, .loading, and .state. A race guard discards stale results, so only the latest fetch ever writes back.

Open Documentation

Parameters

  • source: A tracked input: a node or a computation whose value is passed to the fetcher; the fetcher re-runs whenever it changes. Omit it (pass the fetcher first) for a one-shot fetch. A nil/false source value gates the fetch off until it becomes truthy.
  • fetcher: The untracked yielding work, called as fetcher(value, previous, refetching) where previous is the previously resolved data and refetching flags a manual refetch. Reactive reads inside it are not tracked as dependencies.
  • initialValue: The starting .data value before the first fetch resolves.

Destroy

luau
Async.Destroy<T>(self: Async<T>)

Destroys the async node, cleaning up all resources.

Open Documentation

mutate

luau
Async.mutate<T>(self: Async<T>, value: T)

Optimistically writes data and invalidates any in-flight fetch.

Open Documentation

refetch

luau
Async.refetch<T>(self: Async<T>, refetching: any?)

Re-runs the fetcher with the current source value without writing the source node. An optional refetching payload is forwarded to the fetcher (defaults to true).

Open Documentation