Ashuuu Singhhh
How would you make an Angular service return a promise? Write a code snippet as an example
By Ashuuu Singhhh in Angular on May 25 2018
  • Ashuuu Singhhh
    May, 2018 25

    To add promise functionality to a service, we inject the “$q” dependency in the service, and then use it like so:angular.factory('testService', function($q) {return {getName: function() {var deferred = $q.defer();//API call here that returns datatestAPI.getName().then(function(name) {deferred.resolve(name);});return deferred.promise;}}; });

    • 1