mirror of
https://github.com/tenrok/axios.git
synced 2026-06-20 20:00:40 +03:00
docs: clarify async/await timeout error handling (#7471)
Co-authored-by: Nathanael BOT <nathanaelbot@minidenathanael.home> Co-authored-by: Jay <jasonsaayman@gmail.com>
This commit is contained in:
@@ -1011,15 +1011,25 @@ async function fetchWithTimeout() {
|
|||||||
try {
|
try {
|
||||||
const response = await axios.get("https://example.com/data", {
|
const response = await axios.get("https://example.com/data", {
|
||||||
timeout: 5000, // 5 seconds
|
timeout: 5000, // 5 seconds
|
||||||
|
transitional: {
|
||||||
|
// set to true if you prefer ETIMEDOUT over ECONNABORTED
|
||||||
|
clarifyTimeoutError: false,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log("Response:", response.data);
|
console.log("Response:", response.data);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (axios.isAxiosError(error) && error.code === "ECONNABORTED") {
|
if (axios.isAxiosError(error)) {
|
||||||
console.error("❌ Request timed out!");
|
if (error.code === "ECONNABORTED" || error.code === "ETIMEDOUT") {
|
||||||
} else {
|
console.error("Request timed out. Please try again.");
|
||||||
console.error("❌ Error:", error.message);
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.error("Axios error:", error.message);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.error("Unexpected error:", error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user