search

Home  >  Q&A  >  body text

How to increase the timeout for axios requests?

I'm relatively new to JavaScript/TypeScript (still learning) and have been trying to make requests using fetch and axios but am running into network connectivity issues. The error I receive is:

cause: AggregateError
      at internalConnectMultiple (node:net:1102:18)
      at internalConnectMultiple (node:net:1161:5)
      at Timeout.internalConnectMultipleTimeout (node:net:1644:3)
      at listOnTimeout (node:internal/timers:575:11)
      at process.processTimers (node:internal/timers:514:7) {
    code: 'ENETUNREACH',
    [errors]: [
      Error: connect ENETUNREACH 2001:67c:4e8:f004::9:443 - Local (undefined:undefined)
          at internalConnectMultiple (node:net:1160:40)
          at Timeout.internalConnectMultipleTimeout (node:net:1644:3)
          at listOnTimeout (node:internal/timers:575:11)
          at process.processTimers (node:internal/timers:514:7) {
        errno: -101,
        code: 'ENETUNREACH',
        syscall: 'connect',
        address: '2001:67c:4e8:f004::9',
        port: 443
      }
    ]
  }
}

Node.js v20.3.1

I think the code is unnecessary, just use axios({"url": "https://example.com"}).

The network speed is slow and unstable, but I can access the internet. These requests work just fine with other utilities such as curl or requests in Python (first try using both), so that rules out any issues with my computer or network configuration.

Both fetch and axios actually work, but I'm forced to keep retrying (up to 200 times) until it decides to work. I tried passing { timeout: 0 } and { timeout: 5000 } to the axios constructor without success.

I also tried this, but nothing seems to work:

const source = CancelToken.source();
const timeout = setTimeout(() => {
  source.cancel();
}, 10000);

axios.get(ip + '/config', {cancelToken: source.token}).then((result) => {
  clearTimeout(timeout);
  // ...
});
axios.get('/foo/bar', {
   signal: AbortSignal.timeout(5000) //Aborts request after 5 seconds
}).then(function(response) {
   // ...
});

P粉293550575P粉293550575452 days ago631

reply all(1)I'll reply

  • P粉814160988

    P粉8141609882023-09-07 10:18:52

    You can use the timeout attribute as one of the configuration options that can be passed to axios.

    https://axios-http.com/docs/req_config

    You can also change its default value instead of using the timeout attribute every time:

    const instance = axios.create();
    instance.defaults.timeout = 2500;

    https://axios-http.com/docs/config_defaults

    reply
    0
  • Cancelreply