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

Understanding and implementing a basic promise


Promises are absolutely essential to many of the core aspects of AngularJS. When learning about promises for the first time, the formal terms can be an impediment to their complete understanding as their literal definitions convey very little about how the actual promise components act.

How to do it…

A promise implementation in one of its simplest forms is as follows:

// create deferred object through $q api
var deferred = $q.defer();

// deferred objects are created with a promise attached
var promise = deferred.promise;

// define handlers to execute once promise state becomes definite
promise.then(function success(data) {
  // deferred.resolve() handler
  // in this implementation, data === 'resolved'
}, function error(data) {
  // deferred.reject() handler
  // in this implementation, data === 'rejected'
});

// this function can be called anywhere to resolve the promise
function asyncResolve() {
  deferred.resolve('resolved');
};

// this...