I am calling an API which has pagination implemented.
The response from the API is
{
data {
field1 : "value1",
field2: "value2",
},
paginationKey : {
id: "value for id",
some_other_field: "value for other field"
}
}
The value for the pagination key is specified in the request, and the response value for pagination key becomes the pagination key for the next request.
The value for pagination key will be null for the first request, and the final value of pagination key in the response will be null. So essentially I have to call the API with pagination key value of null, then whatever value of pagination key I get in response, use that for second request and keep continuing until the value of the key in the response becomes null.
My problem is I am making ajax call using JQuery to this API like
let ajaxPromise = $.ajax({
url: requestUrl,
type: 'GET',
data: requestData, // containing the paginationKey like I mentioned above
// other parameters for AJAX call like crossdomain, timeout etc
})
ajaxPromise.then(function(data) {
successCallBack(data);
}, function(error, errorMessage) {
failureCallBack(error, errorMessage)
})
with successCallBack and failureCallBack being methods that I have defined
Now the Ajax call, and the subsequent callbacks being asynchronous in JS, I am having difficulty to make these requests in a loop, and break out of this loop when the response paginationKey becomes null.
How can I achieve this?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…