waitFor

View as Markdown

waitFor

  • waitFor(params): Promise<boolean>

Returns a promise that is resolved only after the current call is in one of the specified states.

Parameters

params
"ended" | "ending" | ("ended" | "ending")[]Required

The call state(s) to wait for.

Returns

Promise<boolean>

A promise that resolves to true if the call is in one of the specified states, or false if the call is ended.

Example

In this example, we dial a phone number and play a TTS message. After the TTS message is finished, we hangup the call and wait for the call to end before printing a message to console.

1import { SignalWire } from "@signalwire/realtime-api";
2
3const client = await SignalWire({ project: "ProjectID Here", token: "Token Here" })
4
5const voiceClient = client.voice;
6
7// Listen for incoming calls
8await voiceClient.listen({
9
10 topics: ["office"],
11 onCallReceived: async (call) => {
12 console.log("Call received");
13 // Answer the call
14 await call.answer();
15
16 // play TTS
17 await call.playTTS({ text: "Hello World" });
18
19 call.hangup();
20
21 call.waitFor("ended").then(() => {
22 console.log("Call ended");
23 });
24 }
25});