1

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

iliasse
  • 247
  • 1
  • 7
  • do you get no response at all or some kind of error? – jogoe Feb 21 '19 at 13:37
  • Have u considered using JS fetch API? – Amiratak88 Feb 21 '19 at 13:40
  • 1
    @Amiratak88 Not a difference, jQuery ajax works with promises as well. – Bergi Feb 21 '19 at 13:42
  • 1
    What exactly do you mean by "*but didn't succeed*"? What works, what exactly does not? Please post the code of the `recursiveJobIdCheck` function. – Bergi Feb 21 '19 at 13:43
  • 1
    "*I did try to put a timeout*" - you'll need to wrap it in a promise and chain to it. Also don't forget to `return` all the promises from all the functions so that you can properly wait for the result. – Bergi Feb 21 '19 at 13:44
  • 1
    @Bergi I know. Just curious why use jQuery for something that the language supports already. – Amiratak88 Feb 21 '19 at 13:44
  • Amiratak88 no I did only use Jquery for this task @Bergi thank you, I updated the post – iliasse Feb 21 '19 at 14:44
  • Thanks for the update. Avoid the [`Promise` constructor antipattern](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it)! You will only need the `new Promise` around `setTimeout`, but everything else should be done through `then` chaining – Bergi Feb 21 '19 at 14:52

1 Answers1

1

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 */)
varun agarwal
  • 1,491
  • 6
  • 9
  • 1
    "*will need*" is a bit too strong - especially for the recursive approach, which can easily be written with `then` syntax. `await` would allow to write it as a `do…while` loop though. – Bergi Feb 21 '19 at 14:32
  • 2
    Btw, I recommend to [avoid `return await`](https://stackoverflow.com/q/43353087/1048572). Apart from that, this answer looks good! – Bergi Feb 21 '19 at 14:33
  • thank you Varun for your answer, I think i have another problem now, after the resolve, when i call the function what's inside the .Then block doesn't get executed : I tried with console.log but I don't see any results even though the execution has completed – iliasse Feb 21 '19 at 14:52
  • @iliasse, what does `recursiveJobIdCheck(job_id).then(res => console.log(res))` print in the console? – varun agarwal Feb 22 '19 at 02:17
  • @varunagarwal Thank you for your reply, nothing is displayed in the console – iliasse Feb 22 '19 at 12:31
  • @iliasse have you tried putting breakpoints in the function and see what get's returned? – varun agarwal Feb 25 '19 at 02:25