asterisk / node-ari-client

Node.js client for ARI. This library is best effort with limited support.
Other
250 stars 98 forks source link

Best practice using callbacks #93

Closed jp1987 closed 6 years ago

jp1987 commented 6 years ago

Our ARI application uses callbacks for most ARI methods, like the following:

dialed.answer(function(err)) {
  bridge.create({type: 'mixing')}, function(err, bridge) {
    // continue application flow
  });
}

We do this throughout our entire application. Another example, stopping a playback:

channel.playBack.stop(function (err) {
  // continue application flow, do stuff like originate
});

However, when I look at this example (for instance): https://github.com/asterisk/ari-examples/blob/master/bridge-dial/example.js

I see that it's done like this:

dialed.answer(function(err) {
  // do nothing
});

bridge.create({type: 'mixing'}, function(err, bridge) {
  // continue application flow
});

Or:

channel.play({media: 'sound:pls-wait-connect-call'}, function(err) {
});

// continue application outside of callback, originate etc

Every now and then, (like I said in #91), we get unhandled rejections. Also, in the above example, it can happen (although rarely) that people hang up DURING the API call of answer, causing bridge create/the adding of channels to fail (because the channel does not exist anymore).

In short: what do you recommend? Make API calls wait for each other and continue dial plan logic in callbacks, or execute the dial plan like in the examples?

Hopefully this makes sense.