O'Reilly logo

AngularJS Services by Jim Lavin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Handling dependencies that return promises

If you've spent some time writing services in AngularJS that interact with Web APIs, you have probably used the $http service. One of the prevalent code patterns that tends to develop involves calling the then method on the promise that is returned from the various $http methods. This, in effect, waits to execute the code until the asynchronous request returns and the promise is resolved.

The following is an example of such a pattern. The retrieveUser function makes a request from the server using the $http service's get method and then handles the response using the then method of the returned promise.

angular.module('brew-anywhere').factory('data_service', ['$http', function ($http) { var data = { retrieveUser: ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required