Re: [fetch] Add timeout option (#20)

@pornel the problem I see with what you wrote is that the `xhr` object in `oldFetch` will NOT have a timeout so it might complete and call `resolve` itself or, also confusing, fail and you'd get two `reject` calls. Am I reading that right? What would stop that?

I think you *have* to set `xhr.timeout = X;` in fetch and then use `xhr.ontimeout` to call `reject` because ultimately the `xhr` object is the source of truth about whether to proceed or not.

Basically you just need to add to `fetch`:

```javascript
xhr.timeout = request.options.timeout || 0;
```
sometime before the `.send` and something like this near where onerror is defined:

```javascript
xhr.ontimeout = function() {
    reject(new TypeError('Request timeout'));
}
```

then this should work as expected...

```javascript
fetch('/users', {
  timeout: 300
}).then(function(response) {console.log('made it!');
}).catch(function(e) {console.log(e);})
```

---
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/fetch/issues/20#issuecomment-135519052

Received on Thursday, 27 August 2015 18:43:50 UTC