【问题标题】:How to set timeout react-native api call using fetch(GET Call)?如何使用 fetch(GET Call) 设置超时 react-native api 调用?
【发布时间】:2018-03-06 11:14:30
【问题描述】:

这是我的代码(用于获取数据)。

loadApi() {
    this.setState({isListLoaded : true})
    return fetch('http://www.androidbegin.com/tutorial/jsonparsetutorial.txt')
       .then((response) => response.json())
       .then((responseJson) => {
         this.setState({listView : responseJson.worldpopulation})
         this.setState({isListLoaded : false})
         ToastAndroid.show('A pikachu appeared nearby !', ToastAndroid.SHORT);
       })
       .catch((error) => {
         console.error(error);
       });
  }

我如何为这个特定的语法设置超时?如果有的话,请分享代码部分。

【问题讨论】:

    标签: android ios react-native networking fetch


    【解决方案1】:

    根据this github线程,到目前为止还没有标准方法。

    但是有一个解决方案,使用whatwg-fetch-timeout

    示例代码:

    return fetch('/path', {timeout: 500}).then(function() {
        // successful fetch 
    }).catch(function(error) {
       // network request failed / timeout 
    })
    

    【讨论】:

      猜你喜欢
      • 2019-07-31
      • 1970-01-01
      • 2019-01-22
      • 2016-10-31
      • 1970-01-01
      • 1970-01-01
      • 2016-08-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多