Home  >  Article  >  Web Front-end  >  HTTP timeout with Axios

HTTP timeout with Axios

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-09-26 08:23:22303browse

HTTP timeout with Axios

Setting up a timeout for HTTP requests can prevent the connection from hanging forever, waiting for the response. It can be set on the client side to improve user experience, and on the server side to improve inter-service communication.

axios package provides a timeout parameter for this functionality.

const HTTP_TIMEOUT = 3000;
const URL = 'https://www.google.com:81';

(async () => {
  try {
    await axios(URL, {
      timeout: HTTP_TIMEOUT,
    });
  } catch (error) {
    console.error('Request timed out', error.cause);
  }
})();

Use this snippet also to simulate aborted requests.

Demo

The demo with the mentioned example is available here.

The above is the detailed content of HTTP timeout with Axios. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn