AngularJS's $q is a promise/deferred implementation.
It's a nice tool to implement asynchronous operations. Below is a small example to show how to use $q.

HTML file

<div ng-app>  
    <div ng-controller="DeferredCtrl">
        <input ng-model="delay"></input>
        <button ng-click="go()">Go</button>
        <input value="{{result}}"></input>
        <div ng-show="error" style="color:red;">{{ error }}</div>


function DeferredCtrl($scope, $q) {  
    $scope.delay = 3000;
    $scope.result = '';
    $scope.go = function() {
        var deferred = $q.defer();
        window.setTimeout(function() {
            var success = Math.random() > 0.5;
            if (success) {
            else {
        }, $scope.delay);
        $scope.error = '';
        deferred.promise.then(function(result) {
            $scope.result = result;
        }, function(error) {
            $scope.error = error;

In the controller, use $q.defer() to create a deferred object. This deferred object is used by client of asynchronous operation.
Once the asynchronous operation is done, use deferred object's resolve function to return the result.
If the asynchronous operation failed, use deferred object's reject function to return the error.

promise property of deferred object can be used to handle result of the asynchronous operation. Use promise object's then(successCallback, errorCallback, notifyCallback) to handle the result.