title: handle promises in javascript
body: <p>I am having trouble when using promise.
  my controller is</p>
  
  <pre><code>angular.module('shoppingPad').controller('customerCtrl', customerCtrl);
  function customerCtrl($scope, customerService){
  
      $scope.addCustomer=function() {
          alert("inside add Customer function");
          var customer={
              name: $scope.name,
              number: $scope.number
          };
           customerService.addCustomer(customer).then(function(response){
            console.log(response);
           },
           function(error){
              console.log(error)
           });
  }
  </code></pre>
  
  <p>this is my service</p>
  
  <pre><code>angular.module('shoppingPad').service('customerService', customerService);
  function customerService($q, $http,restService) {
      //this is deferred object which will resolve or resolve the promise
      var deferred = $q.defer();
      //a function to add customer data to backend used service.it takes customer object as paramater.
      this.addCustomer = function(customer) {
    return restService.postRequest('customers/info',customer,function(response){
          deferred.resolve(response.data); 
         return deferred.promise;
      },
      function(error){
          deferred.reject(error);
          return deferred.promise;
      });
  };
  </code></pre>
  
  <p>This is my restService.js</p>
  
  <pre><code>angular.module('shoppingPad').service('restService',restService);
  function restService($http){
      //set port number and baseUrl here
      var port=3001;
      var baseUrl="http://localhost:"+port;
  
  //generic getRequest function 
  
  
  this.postRequest=function(url,data,successCallback,failureCallback){
      $http({
          method:'POST',
          url:baseUrl+"/"+url,
          data:data
      }).then(function(response){
          successCallback(response);
      },
      function(error){
          alert("internal server error");
          failureCallback(error);
  
      });
  
  };//end of postRequest function
  }//end of service
  </code></pre>
  
  <p>I am getting error in my controller as 
  Error: customerService.addCustomer(...) is undefined.
  if Iam doing wrong please correct me with good code</p>