Skip to main content

Asynchronous state

The root state can be set to a promise value, either as an initial value for hookstate/useHookstate or as a subsequent value via State.set method.

Checking if state is loading

While a promise is not resolved or rejected almost any operation will result in an exception. To check if underlying promise is resolved or rejected, use StateMethods.promised. To check if underlying promise is rejected, use StateMethods.error. For example:


Executing an action when state is loaded

It is also possible to postpone an action or error handling until a promise is settled, which is frequently useful with global states initialised to a promise. To enable this behavior, action callback of the StateMethods.batch method should return the special symbol postpone:

const state = hookstate(new Promise(...));
state.batch((state) => {
if (state.promised) {
return postpone
if (state.error) {
/* do something when promise is rejected */
/* do something when promise is resolved */