An axios call never finishes when the API server stops answering
the request stays pending and the loading spinner never stopsShort 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.
Fix it fast
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))"2Match what you see, then jump to its fix
- 1
The request stays pending until the browser or the server closes the socketNo timeout is configured, so axios waits without limitFix - 2
The 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
The user leaves the page or presses cancel, but the request keeps runningYou need a hard deadline or a cancel button, not only a timeoutFix
- 1
What does the error look like?
the request stays pending and the loading spinner never stopscode: '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 axios2Fix
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
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
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
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Web apps
Axios request is not cancelled when a React component unmounts
A cancelled request is logged as an error in the catch block
Axios cancels a request when you pass it an AbortSignal and later call abort() on its controller.
4 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fixWeb apps
Can't resolve '../axios' in a React project
Module not found: Error: Can't resolve '../axios'
A path that starts with ./ or ../ is a file path, not the axios package.
3 causes, each with a fixWeb apps
Axios returns 401 from a React app but the login works elsewhere
Error: Request failed with status code 401
A 401 means the server received the request without valid credentials.
3 causes, each with a fix