signal : AbortSignal.timeout( 2000 ), method : "GET" , url : `https://jsonplaceholder.typicode.com/posts` .then( ( response ) => { console .log(response.data); .catch( ( error ) => { console .log(error);

In this example, we pass a connection timeout of 2000 milliseconds using the AbortSignal.timeout method (API provided in Node.js v17.3).

Or you can create a helper function like this:

javascriptimport axios from "axios";
function newAbortSignal(timeout) {
  const abortController = new AbortController();
  setTimeout(() => abortController.abort(), timeout || 0);
  return abortController.signal;
axios
  .request({
    signal: newAbortSignal(2000),
    method: "GET",
    url: `https://jsonplaceholder.typicode.com/posts`
  .then((response) => {
    console.log(response.data);
  .catch((error) => {
    console.log(error);

This example uses the AbortController API.

signal: AbortSignal.timeout(2000), method: "GET", url: `https://jsonplaceholder.typicode.com/posts` .then((response) => { console.log(response.data); .catch((error) => { console.log(error);

In this example, we provide response AND connection timeouts to the axios request!

Tim Mouskhelichvili
Hello! I am Tim Mouskhelichvili, a Freelance Developer & Consultant from Montreal, Canada. I specialize in React, Node.js & TypeScript application development. If you need help on a project, please reach out, and let’s work together.
Contact Me Hi, your explanation is really easy to understand but I have a question, where should `axios.defaults.timeout = 2000` be written? In App.jsx, index.jsx or is there somewhere else? REPLY