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

Add non-standardized timeout option to the fetch API #6504

Closed
wants to merge 2 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
34 changes: 34 additions & 0 deletions Libraries/Fetch/__tests__/fetch-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

'use strict';

jest.dontMock('fetch');
const fetch = require('fetch').fetch;

// Let fetch pick up a XMLHttpRequest mock from global scope
global.XMLHttpRequest = jest.fn();

describe('fetch', () => {
it('should only set XMLHttpRequest timeout if set in options', () => {
const url = 'http://foobar/';

fetch(url).then(() => {}, () => {});
jest.runAllTicks();
expect(XMLHttpRequest.mock.instances.length).toBe(1);
expect(XMLHttpRequest.mock.instances[0].timeout).toBeUndefined();

XMLHttpRequest.mockClear();

fetch(url, {timeout: 42}).then(() => {}, () => {});
jest.runAllTicks();
expect(XMLHttpRequest.mock.instances.length).toBe(1);
expect(XMLHttpRequest.mock.instances[0].timeout).toBe(42);
});
});
7 changes: 7 additions & 0 deletions Libraries/Fetch/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ var self = {};
this.method = normalizeMethod(options.method || this.method || 'GET')
this.mode = options.mode || this.mode || null
this.referrer = null
this.timeout = options.timeout || null

if ((this.method === 'GET' || this.method === 'HEAD') && body) {
throw new TypeError('Body not allowed for GET or HEAD requests')
Expand Down Expand Up @@ -376,6 +377,12 @@ var self = {};

var xhr = new XMLHttpRequest()

if (request.timeout) {
// Only set XMLHttpRequest timeout if set in options to
// avoid overriding the XMLHttpRequest default timeout
xhr.timeout = request.timeout
}

function responseURL() {
if ('responseURL' in xhr) {
return xhr.responseURL
Expand Down
13 changes: 13 additions & 0 deletions docs/Network.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ fetch('https://mywebsite.com/endpoint/', {
})
```

A non-standardized timeout option is available in the React Native implementation of the fetch API:

```js
fetch('https://mywebsite.com/endpoint/', {
timeout: 30000
})
```

Notes:
- The timeout is specified in milliseconds.
- The default if unspecified is **no timeout**.
- The returned Promise is rejected if the request timeout.

#### Async

`fetch` returns a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) that can be processed in two ways:
Expand Down