Re: [whatwg/fetch] Aborting a fetch: The Next Generation (#447)

@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.

![fetch-cancel](https://user-images.githubusercontent.com/8843669/28246480-197bb1aa-6a1b-11e7-9d07-adb3e21e89b0.png)

```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