axios 设置 timeout 不生效

it2025-01-12  5

axios 设置 timeout 不生效

目前只是记录一下, 还没有深入研究, 若有不对或其他见解还请大家多多留言补充!

自己封装的方法:

const axiosWithTimeout = async (config) => { const { timeout } = config; const abort = axios.CancelToken.source(); setTimeout( () => abort.cancel(`Timeout of ${timeout}ms.`), timeout, ); return axios(config); };

如何使用

const response = await axiosWithTimeout({ method: 'GET', url: verifyUrl, timeout: reqTimeout, });

另外一种方法似乎也生效(但有部分网友说不生效):

import axios from 'axios'; const httpClient = axios.create(); httpClient.defaults.timeout = 500; const response = await axios.get(verifyUrl);

参考链接: https://stackoverflow.com/questions/36690451/timeout-feature-in-the-axios-library-is-not-working

最新回复(0)