Promises

Promise - proposed 1976 by Daniel P. Friedman. The value is unknown because the computation is not complete.
A javascript object that will, eventually, return a value. Your code does not wait.
Different standards and Implementations.
Promises/A
http://wiki.commonjs.org/wiki/Promises/A
Promises/A+
https://promisesaplus.com/
bluebird
http://bluebirdjs.com/docs/getting-started.html
q, the one used by angular
https://github.com/kriskowal/q
ES6 (ECMAScript, ie javascript)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
This will likely be the standard in the future, part of core javascript.

States and fates

  1. fulfilled
  2. rejected
  3. pending

Fates

  1. resolved
  2. unresolved
Resolved promises can be
fulfilled, rejected, or pending.

Fulfilled if it is finished, rejected if it has been rejected or is currently rejecting. Pending if it has been resolved to another promise.

Unresolved promises are pending.

Commonly found misused idiom

$http.get returns a promise already, there is almost always no need to wrap in $q.defer(). This is the most commonly found misuse. Not really bad, just superflous.
This is called the 'deferred antipattern' or 'Promise constructor antipattern'

A simple promise

$scope.getData = function (url) {
    return $http.get(url).then(
        function success(response) {
            $scope.worked = true
        },
        function failure(reason) {
            $scope.worked = false
        }
    )
}
    

{{ worked }}

Promise Anti Pattern

// In the Controller
    
$scope.getFromFactory = function (url) {
    myFactory.getData(url).then(
        function success() {
            $scope.worked = "Working"
        }, function failure() {
            $scope.worked = "Not working"
        })
}
// In the factory
getData: function (url) {
    return $http.get(url).then(
        function success(response) {
            return response.data
        },
        function failure(reason) {
            return reason // Does not return promise
        }
    )
}

{{ worked }}

Better

// In the Controller
$scope.getFromFactoryGOOD = function (url) {
    myFactory.getDataGOOD(url).then(
        function success() {
            $scope.worked = true
        }, function failure() {
            $scope.worked = false
        })
}
                    
// In the factory
getDataGOOD: function (url) {
    return $http.get(url).then(
        function success(response) {
            console.log(response.data)
            return response.data
        }
    )
}

{{ worked }}

Best!!
function initialize() {
    var _url = "/common/timeZoneData.json";
    return $http({
        method: 'GET',
        url: _url
    }).then(function successCallback(response) {
        for (var i = 0; i < response.data.zones.length; i++) {
            moment.tz.add(response.data.zones[i]);
        }
    }).then(function () {
        return getTasks()
    }).then(function () {
        return addLate()
    }).catch(function () {
        sweetAlertFactory.note("Error getting time zone information!");
    })
}

Full circle back to synchronous code? Not quite, no waiting

Every .then returns a function. This maintains the promise so it is not resolved prematurely.

Catch Method is used. Any failed promise in the chain will break the chain and immediately proceed to the catch method.
Avoids .success and .failure which are convenience methods for $http