If telephone.get is asynchronous, you need to wait for it to complete before you can make your first warning.
document.addEventListener("deviceready", onDeviceReady, false);
Write a devicePhoneNumber function to accept the done callback. The callback receives two parameters: err (if present) and result . Regardless of telephoneNumber.get , the callback will still be called
function devicePhoneNumber(done) { var telephoneNumber = cordova.require("telephonenumber"); telephoneNumber.get(function (result) { done(null, result); }, function () { done(Error("There was an error getting the phone number.")); }); }
To use the function now, pass a callback function that takes two parameters, err and result . Check for an error in your callback. If present, treat it accordingly. You may receive an error using err.message .
function onDeviceReady() { devicePhoneNumber(function(err, result) { if (err) return alert(err.message); alert("Alert 1");
source share