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
 
    