O'Reilly logo

AngularJS Web Application Development Cookbook by Matt Frisbie

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

Chaining promises and promise handlers

Much of the purpose of promises is to allow the developer to serialize and reason about independent asynchronous actions. This can be accomplished by utilizing promise chaining in AngularJS.

Getting ready

Assume that all the examples in this recipe have been set up in the following manner:

var deferred = $q.defer()
    , promise = deferred.promise;

Also, assume that $q and other built-in AngularJS services have already been injected into the current lexical scope.

How to do it…

The promise handler definition method then() returns another promise, which can further have handlers defined upon it in a chain handler, as shown here:

var successHandler = function() { $log.log('called'); }; promise .then(successHandler) .then(successHandler) ...

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