dist/crocodoc.viewer.js
function (url, retries) {
            var req,
                aborted = false,
                ajax = this,
                $deferred = $.Deferred();

            /**
             * If there are retries remaining, make another attempt, otherwise
             * give up and reject the deferred
             * @param   {Object} error The error object
             * @returns {void}
             * @private
             */
            function retryOrFail(error) {
                if (retries > 0) {
                    // if we have retries remaining, make another request
                    retries--;
                    req = request();
                } else {
                    // finally give up
                    $deferred.reject(error);
                }
            }

            /**
             * Make an AJAX request for the asset
             * @returns {XMLHttpRequest|XDomainRequest} Request object
             * @private
             */
            function request() {
                return ajax.request(url, {
                    success: function () {
                        var retryAfter,
                            req;

                        if (!aborted) {
                            req = this.rawRequest;
                            // check status code for 202
                            if (this.status === 202 && util.isFn(req.getResponseHeader)) {
                                // retry the request
                                retryAfter = parseInt(req.getResponseHeader('retry-after'));
                                if (retryAfter > 0) {
                                    setTimeout(request, retryAfter * 1000);
                                    return;
                                }
                            }
                            if (this.responseText) {
                                $deferred.resolve(this.responseText);
                            } else {
                                // the response was empty, so consider this a
                                // failed request
                                retryOrFail({
                                    error: 'empty response',
                                    status: this.status,
                                    resource: url
                                });
                            }
                        }
                    },
                    fail: function () {
                        if (!aborted) {
                            retryOrFail({
                                error: this.statusText,
                                status: this.status,
                                resource: url
                            });
                        }
                    }
                });
            }

            req = request();
            return $deferred.promise({
                abort: function() {
                    aborted = true;
                    req.abort();
                }
            });
        }
function (url, retries) {
            var req,
                aborted = false,
                ajax = this,
                $deferred = $.Deferred();

            /**
             * If there are retries remaining, make another attempt, otherwise
             * give up and reject the deferred
             * @param   {Object} error The error object
             * @returns {void}
             * @private
             */
            function retryOrFail(error) {
                if (retries > 0) {
                    // if we have retries remaining, make another request
                    retries--;
                    req = request();
                } else {
                    // finally give up
                    $deferred.reject(error);
                }
            }

            /**
             * Make an AJAX request for the asset
             * @returns {XMLHttpRequest|XDomainRequest} Request object
             * @private
             */
            function request() {
                return ajax.request(url, {
                    success: function () {
                        var retryAfter,
                            req;

                        if (!aborted) {
                            req = this.rawRequest;
                            // check status code for 202
                            if (this.status === 202 && util.isFn(req.getResponseHeader)) {
                                // retry the request
                                retryAfter = parseInt(req.getResponseHeader('retry-after'));
                                if (retryAfter > 0) {
                                    setTimeout(request, retryAfter * 1000);
                                    return;
                                }
                            }
                            if (this.responseText) {
                                $deferred.resolve(this.responseText);
                            } else {
                                // the response was empty, so consider this a
                                // failed request
                                retryOrFail({
                                    error: 'empty response',
                                    status: this.status,
                                    resource: url
                                });
                            }
                        }
                    },
                    fail: function () {
                        if (!aborted) {
                            retryOrFail({
                                error: this.statusText,
                                status: this.status,
                                resource: url
                            });
                        }
                    }
                });
            }

            req = request();
            return $deferred.promise({
                abort: function() {
                    aborted = true;
                    req.abort();
                }
            });
        }
function ( url , retries ) { var req , aborted = false , ajax = this , $deferred = $ . Deferred ( ) ; /**
             * If there are retries remaining, make another attempt, otherwise
             * give up and reject the deferred
             * @param   {Object} error The error object
             * @returns {void}
             * @private
             */ function retryOrFail ( error ) { if ( retries > 0 ) { // if we have retries remaining, make another request retries -- ; req = request ( ) ; } else { // finally give up $deferred . reject ( error ) ; } } /**
             * Make an AJAX request for the asset
             * @returns {XMLHttpRequest|XDomainRequest} Request object
             * @private
             */ function request ( ) { return ajax . request ( url , { success : function ( ) { var retryAfter , req ; if ( ! aborted ) { req = this . rawRequest ; // check status code for 202 if ( this . status === 202 && util . isFn ( req . getResponseHeader ) ) { // retry the request retryAfter = parseInt ( req . getResponseHeader ( 'retry-after' ) ) ; if ( retryAfter > 0 ) { setTimeout ( request , retryAfter * 1000 ) ; return ; } } if ( this . responseText ) { $deferred . resolve ( this . responseText ) ; } else { // the response was empty, so consider this a // failed request retryOrFail ( { error : 'empty response' , status : this . status , resource : url } ) ; } } } , fail : function ( ) { if ( ! aborted ) { retryOrFail ( { error : this . statusText , status : this . status , resource : url } ) ; } } } ) ; } req = request ( ) ; return $deferred . promise ( { abort : function ( ) { aborted = true ; req . abort ( ) ; } } ) ; }
Fetch an asset, retrying if necessary
@param {string} url      A url for the desired asset
@param {number} retries  The number of times to retry if the request fails
@returns {$.Promise}     A promise with an additional abort() method that will abort the XHR request.
Fetch an asset retrying if necessary