code: function() {
      var pending = [],
          value, deferred;
  
      deferred = {
  
        resolve: function(val) {
          if (pending) {
            var callbacks = pending;
            pending = undefined;
            value = ref(val);
  
            if (callbacks.length) {
              nextTick(function() {
                var callback;
                for (var i = 0, ii = callbacks.length; i < ii; i++) {
                  callback = callbacks[i];
                  value.then(callback[0], callback[1]);
                }
              });
            }
          }
        },
  
  
        reject: function(reason) {
          deferred.resolve(reject(reason));
        },
  
  
        promise: {
          then: function(callback, errback) {
            var result = defer();
  
            var wrappedCallback = function(value) {
              try {
                result.resolve((callback || defaultCallback)(value));
              } catch(e) {
                exceptionHandler(e);
                result.reject(e);
              }
            };
  
            var wrappedErrback = function(reason) {
              try {
                result.resolve((errback || defaultErrback)(reason));
              } catch(e) {
                exceptionHandler(e);
                result.reject(e);
              }
            };
  
            if (pending) {
              pending.push([wrappedCallback, wrappedErrback]);
            } else {
              value.then(wrappedCallback, wrappedErrback);
            }
  
            return result.promise;
          }
        }
      };
  
      return deferred;
    }
docs: @ngdoc
  @name ng.$q#defer
  @methodOf ng.$q
  @description
  Creates a `Deferred` object which represents a task which will finish in the future.
  
  @returns {Deferred} Returns a new instance of deferred.