简体   繁体   English

异步 javascript 承诺使用 settimeout 调用

[英]asynchronous javascript promises call with settimeout

I am trying to achieve the following :我正在努力实现以下目标:

I have 3 APIS to call in order to retrieve DATA:我有 3 个 APIS 可以调用以检索数据:

  • the first API starts a job for retrieving data and returns an id for it第一个 API 启动一个用于检索数据的作业并为其返回一个 id
  • the second API informs me about the state of the job (completed, cancelled...), I need to perform multiple calls to this API before I can call the next one.第二个 API 通知我有关作业的状态(已完成、已取消...),我需要对这个 API 执行多次调用,然后才能调用下一个。
  • the third API is the one that sends me data back when a job is completed.第三个 API 是在作业完成时向我发送数据的 API。

The problem I have is using the second API, I don't succeed at sending back data to my program, here is my code :我的问题是使用第二个 API,我没有成功将数据发送回我的程序,这是我的代码:

function getJobId(token) {
  return  $.ajax({
    url:  "url" + token;
  });
}

function getJobStatus(job_id) {
  var url = "url" + job_id;
  return  $.ajax({
    url: url
  });
}

getJobStatus(job_id).then(function(response) {
  if (response.jobrun_status === "QUEUED" || response.jobrun_status === "INPROGRESS") {
    //setTimeout(recursiveJobIdCheck(job_id), 2000);
    recursiveJobIdCheck(job_id);
  } else {
    console.log(response.jobrun_status);
    return response.jobrun_status;
  }
});

I did try to put a timeout between each call to the second API but didn't succeed, could someone explain to me how I could achieve this while keeping an interval between each request call until the job is finished.我确实尝试在对第二个 API 的每次调用之间设置超时,但没有成功,有人可以向我解释如何实现这一点,同时在每个请求调用之间保持间隔直到作业完成。

Thank you in advance.先感谢您。

Edit : I forgot to add the recursiveJobIdCheck function here it is编辑:我忘了在这里添加 recursiveJobIdCheck 函数

function recursiveJobIdCheck2(job_id) {
  return new Promise((resolve,reject) => {
    getJobStatus(job_id).then(function(response){
      if(response.jobrun_status === "QUEUED" || response.jobrun_status === "INPROGRESS"){
        //setTimeout(recursiveJobIdCheck(job_id), 2000);
        recursiveJobIdCheck2(job_id);
      }
      else{
        if(response.jobrun_status === "COMPLETED"){
        console.log(response.jobrun_status);
         resolve(response.jobrun_status);
         }
         else{
           reject(response.jobrun_status);
         }
      }
    });
  });
}

the calls to the api keep running all the time before it is complete, when I return the value via the Resolve function nothing happens in the main program inside the .then block对 api 的调用在完成之前一直在运行,当我通过 Resolve 函数返回值时,.then 块内的主程序中没有任何反应

You will need async/await to handle recursive api calls to simplify the code.您将需要async/await来处理递归 api 调用以简化代码。

function getJobStatus(job_id){
  var url = "url" + job_id;
  return  $.ajax({
    url : url
  });
}

function queueNextCall () {
  return new Promise(function (resolve, reject) {
    setTimeout(resolve, 2000);
  });
}

async function recursiveJobIdCheck(job_id) {
  var response = await getJobStatus(job_id)
  if(response.jobrun_status === "QUEUED" || response.jobrun_status === "INPROGRESS"){
    await queueNextCall();
    return recursiveJobIdCheck(job_id)
  } else {
    console.log(response.jobrun_status);
    return response.jobrun_status;
  }
}

And all you have to do is call你所要做的就是打电话

recursiveJobIdCheck(job_id).then(/* Success job function */)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM