简体   繁体   中英

asynchronous javascript promises call with settimeout

I am trying to achieve the following :

I have 3 APIS to call in order to retrieve DATA:

  • the first API starts a job for retrieving data and returns an id for it
  • 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.
  • the third API is the one that sends me data back when a job is completed.

The problem I have is using the second API, I don't succeed at sending back data to my program, here is my code :

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.

Thank you in advance.

Edit : I forgot to add the recursiveJobIdCheck function here it is

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

You will need async/await to handle recursive api calls to simplify the code.

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 */)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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