From 9f4f5937112655a9881d3281da8e72035bc8b180 Mon Sep 17 00:00:00 2001 From: David Bennett Date: Sat, 27 Apr 2013 11:22:03 -0400 Subject: feat($http): add support for aborting via timeout promises If the timeout argument is a promise, abort the request when it is resolved. Implemented by adding support to $httpBackend service and $httpBackend mock service. This api can also be used to explicitly abort requests while keeping the communication between the deffered and promise unidirectional. Closes #1159 --- src/ng/httpBackend.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'src/ng/httpBackend.js') diff --git a/src/ng/httpBackend.js b/src/ng/httpBackend.js index 69ac5976..ed8404f9 100644 --- a/src/ng/httpBackend.js +++ b/src/ng/httpBackend.js @@ -107,20 +107,25 @@ function createHttpBackend($browser, XHR, $browserDefer, callbacks, rawDocument, } if (timeout > 0) { - var timeoutId = $browserDefer(function() { - status = -1; - jsonpDone && jsonpDone(); - xhr && xhr.abort(); - }, timeout); + var timeoutId = $browserDefer(timeoutRequest, timeout); + } else if (timeout && timeout.then) { + timeout.then(timeoutRequest); } + function timeoutRequest() { + status = -1; + jsonpDone && jsonpDone(); + xhr && xhr.abort(); + } + function completeRequest(callback, status, response, headersString) { // URL_MATCH is defined in src/service/location.js var protocol = (url.match(SERVER_MATCH) || ['', locationProtocol])[1]; - // cancel timeout + // cancel timeout and subsequent timeout promise resolution timeoutId && $browserDefer.cancel(timeoutId); + jsonpDone = xhr = null; // fix status code for file protocol (it's always 0) status = (protocol == 'file') ? (response ? 200 : 404) : status; -- cgit v1.2.3