Amal p
Amal p

Reputation: 3052

How to set timeout react-native api call using fetch(GET Call)?

This my code(used to fetch data).

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);
       });
  }

How can i set a timeout to this specific syntax ?.Please share the code part if you have any.

Upvotes: 1

Views: 4013

Answers (1)

Ravi
Ravi

Reputation: 35569

There is no standard way till now according to this github thread.

However there is one solution, use whatwg-fetch-timeout

sample code :

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

Upvotes: 1

Related Questions