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 an optional timeout.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/statelyai/xstate/llms.txt
Use this file to discover all available pages before exploring further.
Signature
Parameters
The actor ref to subscribe to and monitor for state changes.
A function that determines if an emitted snapshot satisfies the condition to wait for. Returns
true when the desired state is reached.Returns
A promise that resolves with the first emitted snapshot that satisfies the predicate. The promise rejects if:
- The timeout is exceeded (throws an error with message
"Timeout of {timeout} ms exceeded") - The signal is aborted (rejects with the signal’s reason)
- The actor terminates without satisfying the predicate (throws an error with message
"Actor terminated without satisfying predicate") - The actor emits an error
Usage
Basic usage
With timeout
With AbortSignal
Waiting for specific context values
Notes
waitFor checks the current snapshot immediately before subscribing. If the predicate is already satisfied, it resolves immediately without waiting for future emissions.