Skip to content

Variable: createAsyncAtom

API / @xmachines/play-atom / createAsyncAtom

const createAsyncAtom: <T>(getValue, options?) => ReadonlyAtom<AsyncAtomState<T>> =
createEngineAsyncAtom;

Defined in: play-atom/src/create-atom.ts:64

Builds a read-only atom that holds the state of a promise

The atom starts at pending, and it moves to done or to error.

The getter receives an AbortSignal. The engine aborts it when the atom derives again before the run settles, so a stale answer reaches the atom never. A getter that needs no signal takes no parameter.

Type Parameters

Type Parameter
T

Parameters

ParameterType
getValue(options) => Promise<T>
options?AtomOptions<AsyncAtomState<T>>

Returns

ReadonlyAtom<AsyncAtomState<T>>

Example

const user = createAsyncAtom(() => fetch("/me").then((response) => response.json()));
if (user.get().status === "done") console.log("ready");