Home > database >  Cancel axios request, if response from server comes after 10 seconds
Cancel axios request, if response from server comes after 10 seconds

Time:12-10

I am using axios in my react-native application to make api calls. What I need to do is to cancel the api request and show a Something went wrong screen if 10 seconds have passed and yet there is no response. Let me know if there is a way to do this.

CodePudding user response:

You can configure the timeout (default is about 5 seconds) and also can use this:

for adding a timeout:

const instance = axios.create({
  baseURL: 'https://some-domain.com/api/',
  timeout: 10000, // in miliseconds
  headers: {'X-Custom-Header': 'foobar'}
});

for axios v0.22.0:

const controller = new AbortController();
controller.abort()

for older versions:

CancelToken

Check this for more information

  • Related