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​
Parameter | Type | Description |
---|---|---|
actorRef | TActorRef | The actor ref to subscribe to |
predicate | (emitted: SnapshotFrom<TActorRef>) => boolean | Determines if a value matches the condition to wait for |
options | Partial<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