Book Image

AngularJS Web application development Cookbook

By : Matthew Frisbie
Book Image

AngularJS Web application development Cookbook

By: Matthew Frisbie

Overview of this book

Packed with easy-to-follow recipes, this practical guide will show you how to unleash the full might of the AngularJS framework. Skip straight to practical solutions and quick, functional answers to your problems without hand-holding or slogging through the basics. Avoid antipatterns and pitfalls, and squeeze the maximum amount out of the most powerful parts of the framework, from creating promise-driven applications to building an extensible event bus. Throughout, take advantage of a clear problem-solving approach that offers code samples and explanations of components you should be using in your production applications.
Table of Contents (17 chapters)
AngularJS Web Application Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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)
  .then(successHandler);

deferred.resolve();
// called
// called
// called

Data handoff for chained handlers

Chained handlers can pass data to their subsequent handlers, as follows:

var successHandler = function(val) { 
 ...