Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

An axios call never finishes when the API server stops answering

Errorthe request stays pending and the loading spinner never stops

Short answer

Axios has no timeout unless you give it one: the default is 0, which means wait forever. Set timeout in milliseconds on the request or on an instance, so a hanging server rejects the promise. For a hard deadline use an AbortSignal. When you handle the error, check for ECONNABORTED as well as ETIMEDOUT.

  • Applies to axios 1.x, Node.js 22, React 19, Chrome current
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Point axios at a server that accepts the connection and never replies, with no timeout set. If the promise never settles, the cause below named No timeout applies.

    node -e "require('net').createServer(()=>{}).listen(4000)" &
    node -e "require('axios').get('http://127.0.0.1:4000').then(()=>console.log('done'),e=>console.log('failed',e.code))"
  2. 2Match what you see, then jump to its fix

    1. 1The request stays pending until the browser or the server closes the socketNo timeout is configured, so axios waits without limitFix
    2. 2The timeout does fire, yet your branch for timeouts never runs and the generic error path showsThe error handler looks for ETIMEDOUT, but axios reports ECONNABORTEDFix
    3. 3The user leaves the page or presses cancel, but the request keeps runningYou need a hard deadline or a cancel button, not only a timeoutFix

What does the error look like?

  • the request stays pending and the loading spinner never stops
  • code: 'ECONNABORTED'

Cause 1 of 3

No timeout is configured, so axios waits without limit

1Check

Search the code for timeout. The axios README marks the default as 0, meaning no timeout. An instance created with axios.create has none either, unless you pass one.

grep -rn "timeout" src/ | grep -i axios

2Fix

Set timeout in milliseconds, preferably once on a shared instance. If the request takes longer than that, axios aborts it and the promise rejects.

import axios from 'axios';

export const api = axios.create({
  baseURL: 'https://api.example.com',
  timeout: 8000, // milliseconds
});

3Verify

Run the first-step test again with the timeout set. It now prints failed within the timeout.

node -e "require('axios').get('http://127.0.0.1:4000',{timeout:2000}).then(()=>console.log('done'),e=>console.log('failed',e.code))"

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 3

The error handler looks for ETIMEDOUT, but axios reports ECONNABORTED

1Check

In the README's error table, ECONNABORTED typically means the request timed out, unless transitional.clarifyTimeoutError is set. Only with that option does axios throw ETIMEDOUT. The option is false by default.

grep -rn "ETIMEDOUT\|ECONNABORTED\|clarifyTimeoutError" src/

2Fix

Test for both codes. Alternatively turn on clarifyTimeoutError so that timeouts always carry ETIMEDOUT.

try {
  await api.get('/report');
} catch (error) {
  if (error.code === 'ECONNABORTED' || error.code === 'ETIMEDOUT') {
    // show "server took too long", offer a retry
  } else {
    throw error;
  }
}

// or, on the instance:
// axios.create({ timeout: 8000, transitional: { clarifyTimeoutError: true } })

3Verify

Point the call at the silent server from the first step and confirm the timeout branch runs.

node -e "require('axios').get('http://127.0.0.1:4000',{timeout:1000}).catch(e=>console.log(e.code))"

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 3 of 3

You need a hard deadline or a cancel button, not only a timeout

1Check

timeout is a single number on the request. To stop a request for any other reason, axios accepts an AbortSignal in the signal option, and the web platform can build a signal that fires after a delay.

grep -rn "signal\|AbortController" src/

2Fix

Pass a signal. AbortSignal.timeout(ms) aborts after a delay and its reason is a TimeoutError. For a cancel button, create an AbortController and call abort(). To cancel the timer early, MDN advises an AbortController with setTimeout and clearTimeout.

// hard deadline
await api.get('/report', { signal: AbortSignal.timeout(8000) });

// cancel on demand (for example in a useEffect cleanup)
const controller = new AbortController();
api.get('/report', { signal: controller.signal });
controller.abort();

3Verify

Call abort() or wait for the deadline and watch the network tab: the request is marked cancelled and the promise rejects.

console.log(controller.signal.aborted)

Confirmed by developer.mozilla.org

Did this fix it?

How do I stop it coming back?

  • Create one axios instance with a timeout and import it everywhere, so no call is left without a limit.
  • Choose timeouts per route: a search box and a report export need different values.
  • Show the user a retry button when the timeout branch runs, instead of a spinner that never ends.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.