0

I'm using Ajax with JQuery to fetch data from an API that only returns 100 records at a time. If my query gives a result with more than 100 records, the API will include an "offset" parameter in the response. I have to use this offset parameter in new API call to get the next 100 records. The API will include a new offset parameter if there's even more records to fetch. And so on until all records are fetched.

I know how to push the result to an array, check if the offset parameter is in the result and if so, do another API call and push that result to the same array. But how do I construct a function that does all these ajax calls and let's me know when all the records have been fetched?

Below is how my function looks right now.

function getContracts(offset) {

    var data = {};

    if (offset !== undefined) {
      data["offset"] = offset;
    }

    $.ajax({
      url: url,
      headers: {
        Authorization: apiKey
      },
      data: data,
      success: function(result){

        $.each(result.records, function() {        
            contracts.push(this);
        });

        if (result.hasOwnProperty("offset")) {
          getContracts(result.offset);
        }

      }

    });

  }
Rawland Hustle
  • 781
  • 1
  • 10
  • 16

1 Answers1

0

What does the API return if there are no more records? Why not just do an if/else in your function that does something with returned data?

if (!offset) {

  // Stop running requests and do something else
  tellMeItsDone();

} else {


  // Continue doing requests
  doAnotherAjaxRequest(offset);

}
DubVader
  • 1,032
  • 1
  • 6
  • 8
  • If there are no more records, the API will not include an "offset" object in the response. I've updated my question with my current function. As you can see my function keeps calling itself as long as there's an "offset" object in the response. My question is really how the instance that calls the function can know when all the API calls have been done. Maybe I'm structuring things the wrong way to begin with? – Rawland Hustle Jul 17 '19 at 13:22
  • Perhaps I am misunderstanding. Are you able to successfully make calls to the API, get the data you want, and do what you want, but you just want to know when it stops making calls? Essentially what you are doing is correct. You have to run the calls in succession until something tells you there are no more calls to make, in this instance it's the removal of the offset key from the response. In this instance, you just do an if/else. If offset is present, do another run of the function, if it's not, do something else (like send you an email or show an alert on the screen). – DubVader Jul 17 '19 at 14:10
  • Also, are you sure there are no other keys that are sent to tell you that no more records are available? Most API's will provide like a 'has_more' key or something with a boolean value. – DubVader Jul 17 '19 at 14:10
  • The "offset" key looks like this and it's appended to the returned JSON object if there are more records to fetch: "offset": "recJwIV1oI61f22uT" In that case I have to include "offset" and it's value as a data parameter in another call. **That's not the problem though**. I guess I want to know how to make the function *getContracts* return a promise when all iterations of $.ajax are done? – Rawland Hustle Jul 17 '19 at 14:11
  • I have more functions like *getContracts*. If I want to implement something like in the answer below, I need each function to return a promise when it's done. I.e. when all the iterations of it's AJAX method are done. https://stackoverflow.com/a/16045729/1711950 – Rawland Hustle Jul 17 '19 at 14:18