Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timeout - candidate for spec #68

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
this.method = normalizeMethod(options.method || 'GET')
this.mode = options.mode || null
this.referrer = null
this.timeout = options.timeout || null
}

function decode(body) {
Expand Down Expand Up @@ -217,6 +218,10 @@
})
})

if (self.timeout) {
xhr.timeout = self.timeout
}

xhr.send((self._body === undefined) ? null : self._body)
})
}
Expand Down
6 changes: 6 additions & 0 deletions test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ var routes = {
res.writeHead(308, {'Location': '/hello'});
res.end();
},
'/slow': function(res) {
setTimeout(function () {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('too slow');
}, 10);
},
'/boom': function(res) {
res.writeHead(500, {'Content-Type': 'text/plain'});
res.end('boom');
Expand Down
9 changes: 9 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ test('rejects promise for network error', function() {
})
})

test('rejects promise for timeout', function() {
return fetch('/slow', {
timeout: 5
}).catch(function() {
assert(true)
})
})

// https://fetch.spec.whatwg.org/#headers-class
suite('Headers', function() {
test('headers are case insensitve', function() {
Expand Down Expand Up @@ -42,6 +50,7 @@ suite('Request', function() {
})
})


// https://fetch.spec.whatwg.org/#response-class
suite('Response', function() {
function readBlobAsText(blob) {
Expand Down