mirror of
https://github.com/tenrok/axios.git
synced 2026-05-15 11:59:42 +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)
|
||||
- [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
|
||||
|
||||
Reference in New Issue
Block a user