重试 API 请求直到找到值

编程入门 行业动态 更新时间:2024-10-04 11:20:19

<a href=https://www.elefans.com/category/jswz/34/1755699.html style=重试 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 请求直到找到值

本文发布于:2024-05-31 05:42:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1771334.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重试   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!