Angular: Simple $q usage

HTML

<div ng-app>
    <div ng-controller="TestingCtrl">
         <h2>{{title}}</h2>

    </div>
</div>

JavaScript

function TestingCtrl($scope, $q) {
    $scope.title = 'Angular testing';


    function asyncGreet(name) {
        var deferred = $q.defer();

        setTimeout(function () {
            deferred.notify('About to greet ' + name + '.');

            if (true) { // Change to false to test reject()
                deferred.resolve('Hello, ' + name + '!');
            } else {
                deferred.reject('Greeting ' + name + ' is not allowed.');
            }
        }, 1000);

        return deferred.promise;
    }

    var promise = asyncGreet('Robin Hood');
    promise.then(function (greeting) {
        alert('Success: ' + greeting);
    }, function (reason) {
        alert('Failed: ' + reason);
    }, function (update) {
        alert('Got notification: ' + update);
    });

}