-1

As seen here from Twilio's documentation, how does the following code work? We have a connection class and an on method. If I haven't previously defined what hasEarlyMedia, showRingingIndicator, or playOutgoingRinging mean, then how does the on method know what they mean and what to do with them? Thanks.

connection.on('ringing', function(hasEarlyMedia) {
  showRingingIndicator();
  if (hasEarlyMedia) { playOutgoingRinging(); }
});
j08691
  • 204,283
  • 31
  • 260
  • 272
Jimmy
  • 3,090
  • 12
  • 42
  • 99
  • 1
    `showRingingIndicator` and `playOutgoingRinging` should be defined _somewhere_. `hasEarlyMedia` is an argument that gets passed by the callback mechanism of the `on` method. – Sebastian Simon Jul 16 '18 at 19:43
  • The functions are presumably defined in the Twilio library that you loaded. – Barmar Jul 16 '18 at 19:48
  • You might want to read the text before the example which explains all your questions. – Jonas Wilms Jul 16 '18 at 20:12

2 Answers2

2

Maybe is easier to understand if we rewrite the code like this:


// when the Connection has entered the ringing state,
// call handleRingingEvent (callback function) and pass an argument, 
// a boolean denoting whether there is early media available from the callee
connection.on('ringing', handleRingingEvent);

function handleRingingEvent(hasEarlyMedia) {

    showRingingIndicator();

    if (hasEarlyMedia) {
        playOutgoingRinging();
    }

}

// if not defined somewhere else 
function showRingingIndicator() {
    // do something
}

// if not defined somewhere else     
function playOutgoingRinging() {
    // do something
}

I hope this helps.

Alex Baban
  • 11,312
  • 4
  • 30
  • 44
1

hasEarlyMedia is an argument. Please check

showRingingIndicator(); and playOutgoingRinging(); method must be defined somewhere. Must be function declared in your one of the library which you have included in your file.

Hardik Shah
  • 4,042
  • 2
  • 20
  • 41