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

import { RelayClient } from '@signalwire/sdk';
const client = new RelayClient({
project: process.env.SIGNALWIRE_PROJECT_ID!,
token: process.env.SIGNALWIRE_API_TOKEN!,
contexts: ['default']
});
client.onCall(async (call) => {
await call.answer();
// Wait for a specific play event
const action = await call.play([{ type: 'tts', text: 'Hello!' }]);
const event = await call.waitFor(
'calling.call.play',
(e) => e.params.state === 'finished',
30_000
);
console.log(`Play finished: ${JSON.stringify(event.params)}`);
});
await client.run();