Search code examples
javascriptasynchronousserviceodata

oData service sequence of calls


How to make sequential async requests in chunks of 50 records to a oData service.

 (function () {
    // Multiple api calls
    var oData = "http://localhost:8888/Clinics";
    fetch(oData)
        .then(response => {
            return response.json();
        }).then(data => {
            var nextLink = data['@@odata.nextLink'];
            console.log(data);

        }).catch(err => {
            console.log(err);
        });


})();

I would like to make the next call after I revive the previous data. The idea is to display data in parts. My code returns the first 50 records and the next link successful. I would l like to know how can I effectively make the next sequential requests.


Solution

  • I manage to achieve the result calling the function recursively. I´m sure there is a more effective way to achieve the same results.

    function oDataCall(nextLink) {
        oData = nextLink;
            fetch(oData)
                .then(response => {
                    return response.json();                   
                }).then(data => {
                    console.log(data);
                    nextDataLink = data['@@odata.nextLink'];
                    if (typeof nextDataLink === "undefined") {
                        eod = true;
                    } else {
                        console.log(nextDataLink);
                        oDataCall(nextDataLink);
                    }                      
    
                }).catch(err => {
                    console.log("error: ", err);
                });       
    
    }      
    
    oDataCall("http://localhost:8888/Clinics");