Skip to content

Commit

Permalink
Support abort API
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesplease committed Dec 7, 2017
1 parent 7a692dc commit 93e9cec
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion fetch.js
@@ -1,7 +1,21 @@
(function(self) {
'use strict';

if (self.fetch) {
function canAbortFetch() {
if (!self.AbortController || !self.AbortSignal) {
return false
}

var abortController = new self.AbortController()

var request = new self.Request('http://a', {
signal: abortController.signal
})

return Boolean(request.signal)
}

if (self.fetch && canAbortFetch()) {
return
}

Expand Down Expand Up @@ -443,6 +457,10 @@
reject(new TypeError('Network request failed'))
}

xhr.onabort = function() {
reject(new DOMException('Aborted', 'AbortError'))
}

xhr.open(request.method, request.url, true)

if (request.credentials === 'include') {
Expand All @@ -459,6 +477,10 @@
xhr.setRequestHeader(name, value)
})

if (init.signal && init.signal.addEventListener) {
init.signal.addEventListener('abort', xhr.abort)
}

xhr.send(typeof request._bodyInit === 'undefined' ? null : request._bodyInit)
})
}
Expand Down

0 comments on commit 93e9cec

Please sign in to comment.