title: Doing a Timeout Error with Fetch - React Native
body: <p>I have a user login function that is working. But, I want to incorporate a time out error for the fetch. Is there a way to set up a timer for 5 seconds or so that would stop trying to fetch after such a time? Otherwise, I just get a red screen after a while saying network error.</p>
  <pre><code>_userLogin() {
    var value = this.refs.form.getValue();
    if (value) {
      // if validation fails, value will be null
      if (!this.validateEmail(value.email)) {
        // eslint-disable-next-line no-undef
        Alert.alert('Enter a valid email');
      } else {
        fetch('http://51.64.34.134:5000/api/login', {
          method: 'POST',
          headers: {
            Accept: 'application/json',
            'Content-Type': 'application/json',
          },
          timeout: 5000,
          body: JSON.stringify({
            username: value.email,
            password: value.password,
          }),
        })
          .then((response) =&gt; response.json())
          .then((responseData) =&gt; {
            if (responseData.status == 'success') {
              this._onValueChange(STORAGE_KEY, responseData.data.token);
              Alert.alert('Login Success!');
              this.props.navigator.push({name: 'StartScreen'});
            } else if (responseData.status == 'error') {
              Alert.alert('Login Error', responseData.message);
            }
          })
          .done();
      }
    }
  }
  </code></pre>