From d6bbb3db86f3157e406620da8e68cc24cb8b4be9 Mon Sep 17 00:00:00 2001 From: rohit miryala <39668736+rohitmiryala@users.noreply.github.com> Date: Tue, 30 Dec 2025 16:44:19 +0530 Subject: [PATCH] docs: add async/await timeout handling example (#7250) Co-authored-by: Jay --- README.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/README.md b/README.md index ffbb9fc..035fdc9 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,7 @@ - [Interceptors](#interceptors) - [Multiple Interceptors](#multiple-interceptors) - [Handling Errors](#handling-errors) + - [Handling Timeouts](#handling-timeouts) - [Cancellation](#cancellation) - [AbortController](#abortcontroller) - [CancelToken 👎](#canceltoken-deprecated) @@ -921,6 +922,27 @@ axios.get('/user/12345') }); ``` +## Handling Timeouts + +```js +async function fetchWithTimeout() { + try { + const response = await axios.get('https://example.com/data', { + timeout: 5000 // 5 seconds + }); + + console.log('Response:', response.data); + + } catch (error) { + if (axios.isAxiosError(error) && error.code === 'ECONNABORTED') { + console.error('❌ Request timed out!'); + } else { + console.error('❌ Error:', error.message); + } + } +} +``` + ## Cancellation ### AbortController