首页 > 解决方案 > How to increase timeout for Axios call [React]

问题描述

I'm trying to increase the timeout of an axios call. Unfortunately, because I am doing some ffmpeg conversation, it seems the call is timedout before the process can be done. Below are the console lines. where "file has been converted successful" is the completion of the backend process, and you can see /downloadShare is returned before.

Any idea if I'm doing it right? Code block below.

GET / 200 0.466 ms - 18
GET / 200 0.392 ms - 18
GET /downloadShare?id=xxxxxx - - ms - -
GET / 200 0.408 ms - 18
GET / 200 0.411 ms - 18
GET /downloadShare?id=xxxxx - - ms - -
GET / 200 0.410 ms - 18
GET / 200 0.395 ms - 18
GET / 200 0.396 ms - 18
GET / 200 0.433 ms - 18
file has been converted successfully
GET / 200 0.672 ms - 18
GET / 200 0.588 ms - 18
GET / 200 0.430 ms - 18
file has been converted succesfully

My code

const url = `${ConfigMain.getBackendURL()}/downloadShare?id=${id}`;
    return Axios.get(url,{
        timeout:100000
    })
        .then(response => {
            console.log('aaaaa', response);
        return response.data
        })
        .catch(e => {
                return []
        });

标签: reactjsaxios

解决方案


要增加 axios 超时,您可以创建一个 axios 实例,然后设置所需的超时

const api = axios.create({
  baseURL: url,
  timeout: '8000ms',
});

要进行 API 调用,您可以像这样使用创建的 axios 实例,

api.get(url)

推荐阅读