From b4c47c4229a98786838639097114d2684a456132 Mon Sep 17 00:00:00 2001 From: "Chakib (spike) Benziane" Date: Sat, 11 Jan 2014 21:46:37 +0100 Subject: [PATCH] Using the $timeout promise as suggested by Alexis Tondelier, see previous commit for a more detailed step by step process --- app/scripts/controllers/main.js | 61 +++++++-------------------------- 1 file changed, 13 insertions(+), 48 deletions(-) diff --git a/app/scripts/controllers/main.js b/app/scripts/controllers/main.js index 94a22fe..5dbef12 100644 --- a/app/scripts/controllers/main.js +++ b/app/scripts/controllers/main.js @@ -20,61 +20,26 @@ angular.module('angularPromisesApp') $scope.loadDataWithFailOver = function() { var requestTimeout = $scope.timeout; - var retries = 10; var doRequest = function () { - var timedOut; - - /* - This deferred will handle the timeout of requests - and notify the promise handlers - */ - var tryRequest = $q.defer(); - - - /* - Timeout + retries of requests is actually processed here - with $timeout - */ - var timeout = $timeout(function(){ - timedOut = true; - if (retries === 0) { - tryRequest.resolve('timeout') - console.log('timeout abort request'); - } else { - console.log('trying new request') - retries--; - tryRequest.resolve('timeout') - doRequest(); - } - }, requestTimeout); - - // Using exponential max timeout latency - // }, Math.exp(requestTimeout/100)); - // requestTimeout += 50; - - - /* - If the promise of the request is fulfilled or - failed, then cancel the timeout/retries processing - */ - tryRequest.promise.then(undefined, function(){ - $timeout.cancel(timeout); - }) + requestTimeout += 50; + + // $timeout returns a promise that will be resolved when timeout ends + // We use Math.exp to economize on number of requests + var timeout = $timeout(function(){}, Math.exp(requestTimeout/100)); + + + timeout.then(doRequest); // If the request times out, make a new one // The API call using $http $http.get('/api/ngparis/_all_docs', { params: {include_docs: true}, - timeout: tryRequest.promise // A promise that will abort the request if resolved - }).success(function(result){ - if (timedOut) - return; // Because promises may be executed later in the digest cycle, abort duplicate request - else { - console.log('success') - tryRequest.reject('request success') - } - }) + // If timeout is reached the request is aborted + timeout: timeout + }).finally(function(){ + $timeout.cancel(timeout); // If request is successful abort the timeout + }) }