重试 API 请求直到找到值"/>
重试 API 请求直到找到值
使用以下 API,我需要重试请求,直到 response.data.status 的值 ===“完成”。有人知道如何实现这一目标吗?
const axios = require("axios");
const qs = require("qs");
let data = qs.stringify({
document_key: "038A2E0792CE72020E9BB88380D002EB582A6B3AE5883C34DE53C9F17D415D99",
});
let config = {
method: "post",
maxBodyLength: Infinity,
url: "",
headers: {
Authorization: apiKey,
"Content-Type": "application/x-www-form-urlencoded",
},
data: data,
};
axios
.request(config)
.then((response) => {
console.log(response.data.status);
})
.catch((error) => {
console.log(error.message);
return;
});
想重试 nTimes 直到 response.data.status === "done" 的值
回答如下:const axios = require("axios");
const qs = require("qs");
let data = qs.stringify({
document_key: "038A2E0792CE72020E9BB88380D002EB582A6B3AE5883C34DE53C9F17D415D99",
});
let config = {
method: "post",
maxBodyLength: Infinity,
url: "https://api-free.deepl/v2/document/95BA71197AC66EE4745FF5269CF4399D",
headers: {
Authorization: apiKey,
"Content-Type": "application/x-www-form-urlencoded",
},
data: data,
};
function retryUntillSuccess(config, callback) {
axios
.request(config)
.then((response) => {
callback(null, response.data.status);
})
.catch((error) => {
retryUntillSuccess(options, callback);
});
}
retryUntillSuccess(config, function(err, resp) {
// Your code here...
});
我希望上面的代码能按照你的要求工作。只是一个模拟功能还没有测试。
更多推荐
重试 API 请求直到找到值
发布评论