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' }, ...

Get AngularJS Web Application Development Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.