How do I handle timeouts with `axios` and `async/await`?

Question

Grade: Education Subject: Support
How do I handle timeouts with `axios` and `async/await`?
Asked by:
56 Viewed 56 Answers

Answer (56)

Best Answer
(407)
Use the `timeout` option in your `axios` configuration (e.g., `axios.get('/api/data', { timeout: 5000 })`). If the request takes longer than the specified timeout, `axios` will reject the promise with an error. This error can then be caught within the `catch` block of your `async/await` structure. Consider displaying a user-friendly message indicating the server is unavailable or the connection is slow.