我想使用 axios 重试 5xx 请求。我的主要请求位于 try catch 块的中间。我正在使用 axios-retry 库自动重试 3 次。
我正在使用的 url 会故意抛出 503。但是该请求没有被重试,而是被我的 catch 块捕获。
axiosRetry(axios, {
retries: 3
});
let result;
const url = "https://httpstat.us/503";
const requestOptions = {
url,
method: "get",
headers: {
},
data: {},
};
try {
result = await axios(requestOptions);
} catch (err) {
throw new Error("Failed to retry")
}
}
return result;
原文由 Kay 发布,翻译遵循 CC BY-SA 4.0 许可协议
axios-retry 使用 axios 拦截 器重试 HTTP 请求。它在请求或响应被 then 或 catch 处理之前拦截它们。下面是工作代码片段。