waitFor

View as MarkdownOpen in Claude

Wait for a specific event type on this call, optionally filtered by a predicate function. This is a one-shot listener — it resolves on the first matching event and then removes itself.

Parameters

eventType
stringRequired

The event type string to wait for.

predicate
(event: RelayEvent) => boolean

Optional filter function. If provided, the wait only resolves when the predicate returns true for a received event.

timeout
number | undefined

Maximum milliseconds to wait. Throws an Error if exceeded. undefined waits indefinitely.

Returns

Promise<RelayEvent> — The first event matching the type and optional predicate.

Example

1import { RelayClient } from '@signalwire/sdk';
2
3const client = new RelayClient({
4 project: process.env.SIGNALWIRE_PROJECT_ID!,
5 token: process.env.SIGNALWIRE_TOKEN!,
6 contexts: ['default']
7});
8
9client.onCall(async (call) => {
10 await call.answer();
11
12 // Wait for a specific play event
13 const action = await call.play([{ type: 'tts', text: 'Hello!' }]);
14 const event = await call.waitFor(
15 'calling.call.play',
16 (e) => e.params.state === 'finished',
17 30_000
18 );
19 console.log(`Play finished: ${JSON.stringify(event.params)}`);
20});
21
22await client.run();