How to Add .success() and .error() to Angular Promises

First let’s create a factory that will be responsible for creating deferred
objects and for adding the .success() and .error() methods to promises. The
following block of code is the bread and butter of this tutorial.

Now, instead of calling $q.defer(), you can use promiseFactory.defer() to
create a deferred object that comes decorated with the .success() and
.error() methods we wrote earlier.

Example

We will inject our promiseFactory into a hypothetical authManager factory
whose role is to handle user authentication. We will then create an
asynchronous login() function that allows users to sign in using the $http
service.

The .success() and .error() methods will come in handy later when we want to
neatly separate logic for successful login events as well as failed attempts
when calling the function from a controller.