如何使用fetch(GET Call)设置超时react-native api调用?

问题描述 投票:0回答:1

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

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个回答
0
投票

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

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

示例代码:

return fetch('/path', {timeout: 500}).then(function() {
    // successful fetch 
}).catch(function(error) {
   // network request failed / timeout 
})
© www.soinside.com 2019 - 2024. All rights reserved.