Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.7k views
in Technique[技术] by (71.8m points)

javascript - Check looping progress in axios nodejs

I'm new in javascript so I try to Use an API to delete vocal from a song, so I want to make some check if splitstatus is success I will call function getlink, but if splitstatus is progress I will keep checking it until the splitstatus is success and call function getlink after it. But when I run my code the result only return "check 1". For additional information that splitstatus only have 2 result "success" or "progress" and inside the splitlink is http links

This is my code

var vidid = "";
var splitstatus = "";
var splitlink = "";

function ceksplit(x) {
  setTimeout(function () {
    axios.get(`https://www.lalal.ai/api/check/?id=${x}`).then((res) => {
      splitstatus = res.data.task.state;
    });
  }, 30000);
}

function getlink(x) {
  axios.get(`https://www.lalal.ai/api/check/?id=${x}`).then((res) => {
    splitlink = res.data.split.accompaniment;
  });
}

app.post("/uploads", async(req, res) => {
                await ceksplit(vidid);
                console.log("cek 1");

                if (splitstatus === "progress") {
                  await ceksplit(vidid);
                  console.log("cek 2");
                }
                if (splitstatus === "progress") {
                  await ceksplit(vidid);
                  console.log("cek 3");
                }
                if (splitstatus === "progress") {
                  await ceksplit(vidid);
                  console.log("cek 4");
                }
                if (splitstatus === "progress") {
                  await ceksplit(vidid);
                  console.log("cek 5");
                }
                if (splitstatus === "success") {
                  await getlink(vidid);
                  console.log(splitlink);
                }
});

Can someone help me? hope you guys understand what I'm asking, thanks:D.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Solution

Can be achieved using setInterval

  • setInterval must be removed
  • check for all progress status based on that you can remove setInterval
var vidid = "";
var splitstatus = "progress";
var splitlink = "";

async function ceksplit(x) {
  let res = await axios.get(`https://www.lalal.ai/api/check/?id=${x}`);
  splitstatus = res.data.task.state;
}

async function getlink(x) {
  let res = await axios.get(`https://www.lalal.ai/api/check/?id=${x}`);
  splitlink = res.data.split.accompaniment;
}

app.post("/uploads", async (req, res) => {
  let _interval = setInterval(async () => {
    if (splitstatus === "progress") {
      await ceksplit(vidid);
    } else if (splitstatus === "success") {
      await getlink(vidid);
      clearInterval(_interval);
    } else {
      console.log(splitstatus);
      //  clearInterval(_interval);
    }
  }, 3000);
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...