Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timeout feature in the axios library is not working

I have set axios.defaults.timeout = 1000;

I stopped the server that provides me with the APIs.

But it takes more than 1s to timeout after sending a request.

This is how my request looks:

import axios from 'axios'; axios.defaults.timeout = 1000;  return axios.post(`${ROOT_URL}/login/${role}`, creds).then((response) => {       console.log(response);          if(response.status === 200) {           // If login was successful, set the token in local storage           localStorage.setItem(`${role}_log_toks`, JSON.stringify(response.data));            // Dispatch the success action           dispatch(receiveLogin(response.data));            return response;         }       }).catch(err => {         console.log(err);         // If there was a problem, we want to         // dispatch the error condition         if(err.data && err.status === 404) {           dispatch(loginError(err.data));         } else {           dispatch(loginError('Please check your network connection and try again.'));         }          return err;       }); 

I have also tried:

return axios.post(`${ROOT_URL}/login/${role}`, creds, {timeout: 1000}).then... 

Axios doesn't stop fetching and after 5 - 10 minutes it finally shows network error. I understand that there are other techniques to handle timeout but why doesn't the timeout feature in axios work? What could be the reason that axios doesn't stop fetching?

Axios version 0.9.1

EDIT: As mentioned in the comments, I have also tried:

import axios from 'axios';  const httpClient = axios.create();  httpClient.defaults.timeout = 500;  return httpClient.post(`${ROOT_URL}/login/${role}`, creds)   .then(handleResponse) 
like image 707
shet_tayyy Avatar asked Apr 18 '16 09:04

shet_tayyy


People also ask

How do I use Axios timeout?

In Axios, the default timeout is set to 0. However, Axios allows you to set a custom timeout when required. One way to add a timeout is to pass it to the config object. console.

What is request timeout in NodeJS?

By default, NodeJS has a timeout value of 120 seconds. Sometimes you may need to increase request timeout in NodeJS to process long running requests.


1 Answers

From this axios issue (Thanks to zhuyifan2013 for giving the solution), I've found that axios timeout is response timeout not connection timeout.

Let say you've requested the URL through axios and server is taking long time to respond, in this case the axios timeout will work.

But you don't have internet connection or the IP address or domain name that you're requesting not there, in this case axios timeout will not work.

You've to use the following code

  const source = CancelToken.source();   const timeout = setTimeout(() => {     source.cancel();     // Timeout Logic   }, 10000);      axios.get(ip + '/config', {cancelToken: source.token}).then((result) => {     // Clear The Timeout     clearTimeout(timeout);      // Handle your response   }); 

Please note that if you've valid connection, still the Timeout Logic block will get executed. So you've to clear the timeout.

like image 179
arthankamal Avatar answered Sep 22 '22 16:09

arthankamal