Skip to content
Version: xstate@5.0.0

waitFor() function​

Subscribes to an actor ref and waits for its emitted value to satisfy a predicate, and then resolves with that value. Will throw if the desired state is not reached after a timeout (defaults to 10 seconds).

Signature:

export declare function waitFor<TActorRef extends ActorRef<any, any>>(actorRef: TActorRef, predicate: (emitted: SnapshotFrom<TActorRef>) => boolean, options?: Partial<WaitForOptions>): Promise<SnapshotFrom<TActorRef>>;

Parameters​

ParameterTypeDescription
actorRefTActorRefThe actor ref to subscribe to
predicate(emitted: SnapshotFrom<TActorRef>) => booleanDetermines if a value matches the condition to wait for
optionsPartial<WaitForOptions>(Optional)

Returns:

Promise<SnapshotFrom<TActorRef>>

A promise that eventually resolves to the emitted value that matches the condition

Example​

const state = await waitFor(someService, state => {
return state.hasTag('loaded');
});

state.hasTag('loaded'); // true