- From: Jan Jaap <notifications@github.com>
- Date: Sun, 16 Jul 2017 02:38:53 -0700
- To: whatwg/fetch <fetch@noreply.github.com>
- Cc: Subscribed <subscribed@noreply.github.com>
- Message-ID: <whatwg/fetch/issues/447/315597547@github.com>
@Rob--W my apologies for the misunderstanding, it is correct that the request was not truly cancelled in the `reader.cancel()` solution. I investigated it a bit further and discovered a potential solution for some applications using [window.stop()](https://developer.mozilla.org/nl/docs/Web/API/Window/stop). It will cancel Fetch requests but it has the same effect as pressing the stop button in the browser.

```javascript
var fetch = (function() {
var fetch = window.fetch;
function cancelableFetch() {
var cancelled = false;
var cancel = function() {
if (cancelled) {
return;
}
cancelled = true;
console.warn('Fetch aborted');
if (window.stop) {
window.stop();
}
throw '';
};
var request = fetch.apply(this, arguments);
return {
then: function(resolve) {
return request.then(resolve);
},
cancel: cancel
};
};
return cancelableFetch;
})();
// fetch request
var request = fetch('https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js');
// processing of result
request.then(function(body) {
console.log("Fetch complete:", (typeof body === 'string') ? body.length : body);
}).catch(function(err) {
console.log(err.message);
});
// cancel request
request.cancel();
```
--
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/fetch/issues/447#issuecomment-315597547
Received on Sunday, 16 July 2017 09:39:20 UTC