mirror of
https://github.com/tenrok/axios.git
synced 2026-06-20 20:00:40 +03:00
docs: add async/await timeout handling example (#7250)
Co-authored-by: Jay <jasonsaayman@gmail.com>
This commit is contained in:
@@ -65,6 +65,7 @@
|
|||||||
- [Interceptors](#interceptors)
|
- [Interceptors](#interceptors)
|
||||||
- [Multiple Interceptors](#multiple-interceptors)
|
- [Multiple Interceptors](#multiple-interceptors)
|
||||||
- [Handling Errors](#handling-errors)
|
- [Handling Errors](#handling-errors)
|
||||||
|
- [Handling Timeouts](#handling-timeouts)
|
||||||
- [Cancellation](#cancellation)
|
- [Cancellation](#cancellation)
|
||||||
- [AbortController](#abortcontroller)
|
- [AbortController](#abortcontroller)
|
||||||
- [CancelToken 👎](#canceltoken-deprecated)
|
- [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
|
## Cancellation
|
||||||
|
|
||||||
### AbortController
|
### AbortController
|
||||||
|
|||||||
Reference in New Issue
Block a user