function Deferred() {
        var result, state, listeners = [],
            _this = this;

        function notify() {
            var listener;
            while ((listener = listeners.shift())) {
                var deferred = listener.deferred;
                var methodName = state === "fulfilled" ? "resolve" : "reject"; //$NON-NLS-0$ //$NON-NLS-1$ //$NON-NLS-2$
                var fn = listener[methodName];
                if (typeof fn === "function") { //$NON-NLS-0$
                	settleDeferred(fn, result, deferred);
                } else {
                    deferred[methodName](result);
                }
            }
        }

        function _reject(error) {
            delete _this._parentCancel;
            state = "rejected";
            result = error;
            if (listeners.length) {
                enqueue(notify);
            }
        }

        function _resolve(value) {
            function once(fn) {
                return function(result) {
                    if (!state || state === "assumed") {
                          fn(result);
                    }
                };
            }
            delete _this._parentCancel;
            try {
                var valueThen = value && (typeof value === "object" || typeof value === "function") && value.then;
                if (typeof valueThen === "function") {
                    if (value === _this) {
                        _reject(new TypeError());
                    } else {
                        state = "assumed";
                        var valueCancel = value && value.cancel;
                        if (typeof valueCancel !== "function") {
                            var deferred = new Deferred();
                            value = deferred.promise;
                            try {
                                valueThen(deferred.resolve, deferred.reject, deferred.progress);
                            } catch (thenError) {
                                deferred.reject(thenError);
                            }
                            valueCancel = value.cancel;
                            valueThen = value.then;
                        }
                        result = value;
                        valueThen.call(value, once(_resolve), once(_reject));
                        _this._parentCancel = valueCancel.bind(value);
                    }
                } else {
                    state = "fulfilled";
                    result = value;
                    if (listeners.length) {
                        enqueue(notify);
                    }
                }
            } catch (error) {
                once(_reject)(error);
            }
        }

        function cancel() {
            var parentCancel = _this._parentCancel;
            if (parentCancel) {
                delete _this._parentCancel;
                parentCancel();
            } else if (!state) {
                var cancelError = new Error("Cancel");
                cancelError.name = "Cancel";
                _reject(cancelError);
            }
        }


        /**
         * Resolves this Deferred.
         * @name resolve
         * @function
         * @memberOf orion.Deferred.prototype
         * @param {Object} value
         * @returns {orion.Promise}
         */
        this.resolve = function(value) {
            if (!state) {
                _resolve(value);
            }
            return _this;
        };

        /**
         * Rejects this Deferred.
         * @name reject
         * @function
         * @memberOf orion.Deferred.prototype
         * @param {Object} error
         * @param {Boolean} [strict]
         * @returns {orion.Promise}
         */
        this.reject = function(error) {
            if (!state) {
                _reject(error);
            }
            return _this;
        };

        /**
         * Notifies listeners of progress on this Deferred.
         * @name progress
         * @function
         * @memberOf orion.Deferred.prototype
         * @param {Object} update The progress update.
         * @returns {orion.Promise}
         */
        this.progress = function(update) {
            if (!state) {
                listeners.forEach(function(listener) {
                    if (listener.progress) {
                        try {
                            listener.progress(update);
                        } catch (ignore) {
                            // ignore
                        }
                    }
                });
            }
            return _this.promise;
        };

        this.cancel = function() {
            if (_this._parentCancel) {
                setTimeout(cancel, 0);
            } else {
                cancel();
            }
            return _this;
        };

        // Note: "then" ALWAYS returns before having onResolve or onReject called as per http://promises-aplus.github.com/promises-spec/
        this.then = function(onFulfill, onReject, onProgress) {
        	var deferred = new Deferred();
            deferred._parentCancel = _this.promise.cancel;
            listeners.push({
                resolve: onFulfill,
                reject: onReject,
                progress: onProgress,
                deferred: deferred
            });
            if (state === "fulfilled" || state === "rejected") {
                enqueue(notify);
            }
            return deferred.promise;
        };

        /**
         * The promise exposed by this Deferred.
         * @name promise
         * @field
         * @memberOf orion.Deferred.prototype
         * @type orion.Promise
         */
        this.promise = {
            then: _this.then,
            cancel: _this.cancel
        };
    }
/**
     * @name orion.Deferred
     * @borrows orion.Promise#then as #then
     * @borrows orion.Promise#cancel as #cancel
     * @class Provides abstraction over asynchronous operations.
     * @description Deferred provides abstraction over asynchronous operations.
     *
     * <p>Because Deferred implements the {@link orion.Promise} interface, a Deferred may be used anywhere a Promise is called for.
     * However, in most such cases it is recommended to use the Deferred's {@link #promise} field instead, which exposes a 
     * simplified, minimally <a href="https://github.com/promises-aplus/promises-spec">Promises/A+</a>-compliant interface to callers.</p>
     */
@name orion.Deferred
@borrows orion.Promise#then as #then
@borrows orion.Promise#cancel as #cancel
@class Provides abstraction over asynchronous operations.
@description Deferred provides abstraction over asynchronous operations.

Because Deferred implements the  orion.Promise interface, a Deferred may be used anywhere a Promise is called for.
However, in most such cases it is recommended to use the Deferred's  #promise field instead, which exposes a
simplified, minimally Promises/A+-compliant interface to callers.
@name orion . Deferred @borrows orion . Promise#then as #then @borrows orion . Promise#cancel as #cancel @class Provides abstraction over asynchronous operations . @description Deferred provides abstraction over asynchronous operations . Because Deferred implements the orion . Promise interface a Deferred may be used anywhere a Promise is called for . However in most such cases it is recommended to use the Deferred ' s #promise field instead which exposes a simplified minimally Promises / A + - compliant interface to callers .
function Deferred() {
        var result, state, listeners = [],
            _this = this;
        function notify() {
            var listener;
            while ((listener = listeners.shift())) {
                var deferred = listener.deferred;
                var methodName = state === "fulfilled" ? "resolve" : "reject"; 
                var fn = listener[methodName];
                if (typeof fn === "function") { 
                	settleDeferred(fn, result, deferred);
                } else {
                    deferred[methodName](result);
                }
            }
        }
        function _reject(error) {
            delete _this._parentCancel;
            state = "rejected";
            result = error;
            if (listeners.length) {
                enqueue(notify);
            }
        }
        function _resolve(value) {
            function once(fn) {
                return function(result) {
                    if (!state || state === "assumed") {
                          fn(result);
                    }
                };
            }
            delete _this._parentCancel;
            try {
                var valueThen = value && (typeof value === "object" || typeof value === "function") && value.then;
                if (typeof valueThen === "function") {
                    if (value === _this) {
                        _reject(new TypeError());
                    } else {
                        state = "assumed";
                        var valueCancel = value && value.cancel;
                        if (typeof valueCancel !== "function") {
                            var deferred = new Deferred();
                            value = deferred.promise;
                            try {
                                valueThen(deferred.resolve, deferred.reject, deferred.progress);
                            } catch (thenError) {
                                deferred.reject(thenError);
                            }
                            valueCancel = value.cancel;
                            valueThen = value.then;
                        }
                        result = value;
                        valueThen.call(value, once(_resolve), once(_reject));
                        _this._parentCancel = valueCancel.bind(value);
                    }
                } else {
                    state = "fulfilled";
                    result = value;
                    if (listeners.length) {
                        enqueue(notify);
                    }
                }
            } catch (error) {
                once(_reject)(error);
            }
        }
        function cancel() {
            var parentCancel = _this._parentCancel;
            if (parentCancel) {
                delete _this._parentCancel;
                parentCancel();
            } else if (!state) {
                var cancelError = new Error("Cancel");
                cancelError.name = "Cancel";
                _reject(cancelError);
            }
        }
        this.resolve = function(value) {
            if (!state) {
                _resolve(value);
            }
            return _this;
        };
        this.reject = function(error) {
            if (!state) {
                _reject(error);
            }
            return _this;
        };
        this.progress = function(update) {
            if (!state) {
                listeners.forEach(function(listener) {
                    if (listener.progress) {
                        try {
                            listener.progress(update);
                        } catch (ignore) {
                        }
                    }
                });
            }
            return _this.promise;
        };
        this.cancel = function() {
            if (_this._parentCancel) {
                setTimeout(cancel, 0);
            } else {
                cancel();
            }
            return _this;
        };
        this.then = function(onFulfill, onReject, onProgress) {
        	var deferred = new Deferred();
            deferred._parentCancel = _this.promise.cancel;
            listeners.push({
                resolve: onFulfill,
                reject: onReject,
                progress: onProgress,
                deferred: deferred
            });
            if (state === "fulfilled" || state === "rejected") {
                enqueue(notify);
            }
            return deferred.promise;
        };
        this.promise = {
            then: _this.then,
            cancel: _this.cancel
        };
    }
function Deferred ( ) { var result , state , listeners = [ ] , _this = this ; function notify ( ) { var listener ; while ( ( listener = listeners . shift ( ) ) ) { var deferred = listener . deferred ; var methodName = state === "fulfilled" ? "resolve" : "reject" ; var fn = listener [ methodName ] ; if ( typeof fn === "function" ) { settleDeferred ( fn , result , deferred ) ; } else { deferred [ methodName ] ( result ) ; } } } function _reject ( error ) { delete _this . _parentCancel ; state = "rejected" ; result = error ; if ( listeners . length ) { enqueue ( notify ) ; } } function _resolve ( value ) { function once ( fn ) { return function ( result ) { if ( ! state || state === "assumed" ) { fn ( result ) ; } } ; } delete _this . _parentCancel ; try { var valueThen = value && ( typeof value === "object" || typeof value === "function" ) && value . then ; if ( typeof valueThen === "function" ) { if ( value === _this ) { _reject ( new TypeError ( ) ) ; } else { state = "assumed" ; var valueCancel = value && value . cancel ; if ( typeof valueCancel !== "function" ) { var deferred = new Deferred ( ) ; value = deferred . promise ; try { valueThen ( deferred . resolve , deferred . reject , deferred . progress ) ; } catch ( thenError ) { deferred . reject ( thenError ) ; } valueCancel = value . cancel ; valueThen = value . then ; } result = value ; valueThen . call ( value , once ( _resolve ) , once ( _reject ) ) ; _this . _parentCancel = valueCancel . bind ( value ) ; } } else { state = "fulfilled" ; result = value ; if ( listeners . length ) { enqueue ( notify ) ; } } } catch ( error ) { once ( _reject ) ( error ) ; } } function cancel ( ) { var parentCancel = _this . _parentCancel ; if ( parentCancel ) { delete _this . _parentCancel ; parentCancel ( ) ; } else if ( ! state ) { var cancelError = new Error ( "Cancel" ) ; cancelError . name = "Cancel" ; _reject ( cancelError ) ; } } this . resolve = function ( value ) { if ( ! state ) { _resolve ( value ) ; } return _this ; } ; this . reject = function ( error ) { if ( ! state ) { _reject ( error ) ; } return _this ; } ; this . progress = function ( update ) { if ( ! state ) { listeners . forEach ( function ( listener ) { if ( listener . progress ) { try { listener . progress ( update ) ; } catch ( ignore ) { } } } ) ; } return _this . promise ; } ; this . cancel = function ( ) { if ( _this . _parentCancel ) { setTimeout ( cancel , 0 ) ; } else { cancel ( ) ; } return _this ; } ; this . then = function ( onFulfill , onReject , onProgress ) { var deferred = new Deferred ( ) ; deferred . _parentCancel = _this . promise . cancel ; listeners . push ( { resolve : onFulfill , reject : onReject , progress : onProgress , deferred : deferred } ) ; if ( state === "fulfilled" || state === "rejected" ) { enqueue ( notify ) ; } return deferred . promise ; } ; this . promise = { then : _this . then , cancel : _this . cancel } ; }
@name orion.Deferred
 @borrows orion.Promise#then as #then
 @borrows orion.Promise#cancel as #cancel
 @class Provides abstraction over asynchronous operations.
@name orion . Deferred @borrows orion . Promise#then as #then @borrows orion . Promise#cancel as #cancel @class Provides abstraction over asynchronous operations .
//$NON-NLS-0$ //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-0$ /**
         * Resolves this Deferred.
         * @name resolve
         * @function
         * @memberOf orion.Deferred.prototype
         * @param {Object} value
         * @returns {orion.Promise}
         */ /**
         * Rejects this Deferred.
         * @name reject
         * @function
         * @memberOf orion.Deferred.prototype
         * @param {Object} error
         * @param {Boolean} [strict]
         * @returns {orion.Promise}
         */ /**
         * Notifies listeners of progress on this Deferred.
         * @name progress
         * @function
         * @memberOf orion.Deferred.prototype
         * @param {Object} update The progress update.
         * @returns {orion.Promise}
         */ // ignore // Note: "then" ALWAYS returns before having onResolve or onReject called as per http://promises-aplus.github.com/promises-spec/ /**
         * The promise exposed by this Deferred.
         * @name promise
         * @field
         * @memberOf orion.Deferred.prototype
         * @type orion.Promise
         */