Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Having issues trying to get timeout method of axios working.

For testing: I setup a intentionally bad API endpoint: it accepts a request, throws an error (Eg: throw new Error(“testing for timeout”) ) and intentionally does nothing else.

My client app (reactJS) hangs once I make a call to the test API endpoint - I expected it to timeout within 2 seconds (my set timeout). I can verify that the app is making contact with server. Its only when I kill my test API server that my client app immediately continues.

Sample code:

const axios = require('axios')
const test1Press = async () => {
    await axios.post('https://mynodeserver.com/api/debug/throw', {timeout: 2000})
    console.log("post call passed")
  catch (err)
    console.log("post call failed")

EDIT (~2020):

On further research, looks like axios timeout is only for response timeouts but not connection timeouts. Suggested solutions for connection timeouts are cancellation methods (e.g. signal, cancelToken (deprecated)):

Tested this and working:

const source = CancelToken.source();
try {
  let response = null;
  setTimeout(() => {
    if (response === null) {
      source.cancel();
  }, 2000);
  response = await axios.post('/url',null,{cancelToken: source.token});
  // success
} catch (error) {
  // fail

With await axios.post('/debug/throw', {timeout: 2000}), actually you send the payload {timeout: 2000} to the server, not set the timeout to 2 seconds. See an example here.

I tested with another syntax of axios and it worked

const test1Press = async () => {
    console.log("test1 pressed")
    // obviously not the actual url in this stackoverflow post
    axios.defaults.baseURL = 'http://localhost:9000'
    console.log("pre call")
    console.log(new Date().toUTCString());
    try {
        await axios({
            method: 'post',
            url: '/',
            timeout: 2000 // only wait for 2s
        console.log(new Date().toUTCString());
        console.log("post call passed")
    catch (err) {
        console.log(new Date().toUTCString());
        console.log("post call failed")
test1Press();

On the server-side, I wait for 5 seconds to have the timeout error on the client-side

const http = require('http');
const server = http.createServer(function (req, res) {
    setTimeout(function () {
        res.write('Hello World!');
        res.end();
    }, 5 * 1 * 1000); // after 5s
    .listen(9000);

Running the code above gives me the timeout error after 2 seconds

test1 pressed
pre call
Mon, 28 Jun 2021 09:01:54 GMT
Mon, 28 Jun 2021 09:01:56 GMT
post call failed
I tested to create the instance of axios, this gives me the same result:

const test1Press = async () => {
    console.log("test1 pressed")
    // obviously not the actual url in this stackoverflow post
    const instance = axios.create({
        baseURL: 'http://localhost:9000',
        timeout: 2000,
    console.log("pre call")
    console.log(new Date().toUTCString());
    try {
        await instance.post('/');
        console.log(new Date().toUTCString());
        console.log("post call passed")
    catch (err) {
        console.log(new Date().toUTCString());
        console.log("post call failed")
test1Press();
                Your second example is the same as my last. I've changed my server code to match yours so it's now "sleeping" instead of throwing an error.. and still my app waits for 5 seconds and I don't get the 2 second failed catch block - with both versions. Hmmm. I don't think then this is a syntax issue; could be related to some sort of conflict with axios/react/other
– andrew
                Jun 28, 2021 at 10:54
                I'm using https - ie url of myurl.com/api/debug/throw    would https cause an issue with timeout ?
– andrew
                Jun 28, 2021 at 11:00
                I don't think it's related to https/http. I didn't test with react, might be it's the difference. Can you do a test with only axios ?
– Đăng Khoa Đinh
                Jun 28, 2021 at 11:07
                this is where my limited experience with react/javascript/axios comes into play :( we are committed heavily to a react solution but I could replace axios with another package; I might try that
– andrew
                Jun 28, 2021 at 11:10
                and this link shows there was issues with timeout in May 2020: github.com/axios/axios/issues/647 - this may not have been fixed yet; I may need to try the cancel token route
– andrew
                Jun 28, 2021 at 11:29

As indicated in @arthankamal answer, timeout property is only responsible for response timeouts but not connection timeouts. To address connection timeouts, use axios's cancellation methods. Adding examples below based on newer signal property (cancelToken is deprecated ).

Example with signal and AbortSignal.timeout() built-in [requires nodejs 17.3+]:

axios({
  method: 'get',
  url: '/foo/bar',
  timeout: 5000,
  signal: AbortSignal.timeout(5000) //Aborts request after 5 seconds
.then(function(response) {
  //...

Example with signal and a timeout helper function:

function newAbortSignal(timeoutMs) {
  const abortController = new AbortController();
  setTimeout(() => abortController.abort(), timeoutMs || 0);
  return abortController.signal;
axios({
  method: 'get',
  url: '/foo/bar',
  timeout: 5000,
  signal: newAbortSignal(5000) //Aborts request after 5 seconds
.then(function(response) {
  //...

Let's 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.

See original answer for solution

Link is a different issue. I can verify that a connection is made. So internet connection/IP Address/domain name are all ok; i've got the server logging that a request was made. So in this case the axios time out should work, but it's not – andrew Jun 28, 2021 at 6:16

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.