Skip to content

Commit 5010640

Browse files
committed
Timeout docs
1 parent 8bb410a commit 5010640

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

docs/index.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -353,12 +353,27 @@ For more information, see the Mozilla Developer Network [xhr.responseType docs](
353353

354354
To abort requests simply invoke the `req.abort()` method.
355355

356-
## Request timeouts
356+
## Timeouts
357357

358-
A timeout can be applied by invoking `req.timeout(ms)`, after which an error
359-
will be triggered. To differentiate between other errors the `err.timeout` property
360-
is set to the `ms` value. __NOTE__ that this is a timeout applied to the request
361-
and all subsequent redirects, not per request.
358+
Sometimes networks and servers get "stuck" and never respond after accepting a request. Set timeouts to avoid requests waiting forever.
359+
360+
* `req.timeout({deadline:ms})` or `req.timeout(ms)` (where `ms` is a number of milliseconds > 0) sets a deadline for the entire request (including all redirects) to complete. If the response isn't fully downloaded within that time, the request will be aborted.
361+
362+
* `req.timeout({response:ms})` sets maximum time to wait for the first byte to arrive from the server, but it does not limit how long the entire download can take. Response timeout should be a few seconds longer than just the time it takes server to respond, because it also includes time to make DNS lookup, TCP/IP and TLS connections.
363+
364+
You should use both `deadline` and `response` timeouts. This way you can use a short response timeout to detect unresponsive networks quickly, and a long deadline to give time for downloads on slow, but reliable, networks.
365+
366+
request
367+
.get('/big-file?network=slow')
368+
.timeout({
369+
response: 5000, // Wait 5 seconds for the server to start sending,
370+
deadline: 60000, // but allow 1 minute for the file to finish loading.
371+
})
372+
.end(function(err, res){
373+
if (err.timeout) { /* timed out! */ }
374+
});
375+
376+
Timeout errors have a `.timeout` property.
362377

363378
## Authentication
364379

0 commit comments

Comments
 (0)