Make repeated ajax calls inside loop until condition is met
up vote
0
down vote
favorite
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?
javascript jquery ajax
add a comment |
up vote
0
down vote
favorite
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?
javascript jquery ajax
Is this through a public api you can share link to ? Are you wanting all the data pushed into a single array ?
– charlietfl
Nov 10 at 15:45
Sorry, I can not share the API as it is not public. Yes, I want to store the data in a single array, but also update the result stored inside the array based on the response returned from every call.
– anuyog
Nov 10 at 16:27
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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?
javascript jquery ajax
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?
javascript jquery ajax
javascript jquery ajax
asked Nov 10 at 15:35
anuyog
183
183
Is this through a public api you can share link to ? Are you wanting all the data pushed into a single array ?
– charlietfl
Nov 10 at 15:45
Sorry, I can not share the API as it is not public. Yes, I want to store the data in a single array, but also update the result stored inside the array based on the response returned from every call.
– anuyog
Nov 10 at 16:27
add a comment |
Is this through a public api you can share link to ? Are you wanting all the data pushed into a single array ?
– charlietfl
Nov 10 at 15:45
Sorry, I can not share the API as it is not public. Yes, I want to store the data in a single array, but also update the result stored inside the array based on the response returned from every call.
– anuyog
Nov 10 at 16:27
Is this through a public api you can share link to ? Are you wanting all the data pushed into a single array ?
– charlietfl
Nov 10 at 15:45
Is this through a public api you can share link to ? Are you wanting all the data pushed into a single array ?
– charlietfl
Nov 10 at 15:45
Sorry, I can not share the API as it is not public. Yes, I want to store the data in a single array, but also update the result stored inside the array based on the response returned from every call.
– anuyog
Nov 10 at 16:27
Sorry, I can not share the API as it is not public. Yes, I want to store the data in a single array, but also update the result stored inside the array based on the response returned from every call.
– anuyog
Nov 10 at 16:27
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Since you need to wait for a call to finish before initiating a new call, you can use a standard loop. A simple solution is to call the API once a call is done, and the key is not null:
const repeatedAPICall = (requestData, successCallBack, failureCallBack) =>
const 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((data) =>
successCallBack(data)
if(data.paginationKey)
repeatedAPICall(data.paginationKey, successCallBack, failureCallBack)
, (error, errorMessage) =>
failureCallBack(error, errorMessage)
)
// 1st call
repeatedAPICall(null, successCallBack, failureCallBack)
If you need an array of pages, you can use async/await in a for...of loop. For every key that is not null, we add the key to the array. If there's a key in the array, we make an api call, and add the result to the results array.
async function repeatedAPICall(
apiCall,
startValue
)
const keys = [startValue];
const result = ;
for (const callKey of keys)
const data = await apiCall(callKey);
result.push(data);
if (data.key !== null) keys.push(data.key);
return result;
// mock api call
const apiCall = ((counter) => () => Promise.resolve(
key: counter < 5 ? counter++ : null
))(0);
repeatedAPICall(apiCall, null)
.then(result => console.log(result)); // use your callbacks hereadd a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Since you need to wait for a call to finish before initiating a new call, you can use a standard loop. A simple solution is to call the API once a call is done, and the key is not null:
const repeatedAPICall = (requestData, successCallBack, failureCallBack) =>
const 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((data) =>
successCallBack(data)
if(data.paginationKey)
repeatedAPICall(data.paginationKey, successCallBack, failureCallBack)
, (error, errorMessage) =>
failureCallBack(error, errorMessage)
)
// 1st call
repeatedAPICall(null, successCallBack, failureCallBack)
If you need an array of pages, you can use async/await in a for...of loop. For every key that is not null, we add the key to the array. If there's a key in the array, we make an api call, and add the result to the results array.
async function repeatedAPICall(
apiCall,
startValue
)
const keys = [startValue];
const result = ;
for (const callKey of keys)
const data = await apiCall(callKey);
result.push(data);
if (data.key !== null) keys.push(data.key);
return result;
// mock api call
const apiCall = ((counter) => () => Promise.resolve(
key: counter < 5 ? counter++ : null
))(0);
repeatedAPICall(apiCall, null)
.then(result => console.log(result)); // use your callbacks hereadd a comment |
up vote
0
down vote
Since you need to wait for a call to finish before initiating a new call, you can use a standard loop. A simple solution is to call the API once a call is done, and the key is not null:
const repeatedAPICall = (requestData, successCallBack, failureCallBack) =>
const 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((data) =>
successCallBack(data)
if(data.paginationKey)
repeatedAPICall(data.paginationKey, successCallBack, failureCallBack)
, (error, errorMessage) =>
failureCallBack(error, errorMessage)
)
// 1st call
repeatedAPICall(null, successCallBack, failureCallBack)
If you need an array of pages, you can use async/await in a for...of loop. For every key that is not null, we add the key to the array. If there's a key in the array, we make an api call, and add the result to the results array.
async function repeatedAPICall(
apiCall,
startValue
)
const keys = [startValue];
const result = ;
for (const callKey of keys)
const data = await apiCall(callKey);
result.push(data);
if (data.key !== null) keys.push(data.key);
return result;
// mock api call
const apiCall = ((counter) => () => Promise.resolve(
key: counter < 5 ? counter++ : null
))(0);
repeatedAPICall(apiCall, null)
.then(result => console.log(result)); // use your callbacks hereadd a comment |
up vote
0
down vote
up vote
0
down vote
Since you need to wait for a call to finish before initiating a new call, you can use a standard loop. A simple solution is to call the API once a call is done, and the key is not null:
const repeatedAPICall = (requestData, successCallBack, failureCallBack) =>
const 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((data) =>
successCallBack(data)
if(data.paginationKey)
repeatedAPICall(data.paginationKey, successCallBack, failureCallBack)
, (error, errorMessage) =>
failureCallBack(error, errorMessage)
)
// 1st call
repeatedAPICall(null, successCallBack, failureCallBack)
If you need an array of pages, you can use async/await in a for...of loop. For every key that is not null, we add the key to the array. If there's a key in the array, we make an api call, and add the result to the results array.
async function repeatedAPICall(
apiCall,
startValue
)
const keys = [startValue];
const result = ;
for (const callKey of keys)
const data = await apiCall(callKey);
result.push(data);
if (data.key !== null) keys.push(data.key);
return result;
// mock api call
const apiCall = ((counter) => () => Promise.resolve(
key: counter < 5 ? counter++ : null
))(0);
repeatedAPICall(apiCall, null)
.then(result => console.log(result)); // use your callbacks hereSince you need to wait for a call to finish before initiating a new call, you can use a standard loop. A simple solution is to call the API once a call is done, and the key is not null:
const repeatedAPICall = (requestData, successCallBack, failureCallBack) =>
const 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((data) =>
successCallBack(data)
if(data.paginationKey)
repeatedAPICall(data.paginationKey, successCallBack, failureCallBack)
, (error, errorMessage) =>
failureCallBack(error, errorMessage)
)
// 1st call
repeatedAPICall(null, successCallBack, failureCallBack)
If you need an array of pages, you can use async/await in a for...of loop. For every key that is not null, we add the key to the array. If there's a key in the array, we make an api call, and add the result to the results array.
async function repeatedAPICall(
apiCall,
startValue
)
const keys = [startValue];
const result = ;
for (const callKey of keys)
const data = await apiCall(callKey);
result.push(data);
if (data.key !== null) keys.push(data.key);
return result;
// mock api call
const apiCall = ((counter) => () => Promise.resolve(
key: counter < 5 ? counter++ : null
))(0);
repeatedAPICall(apiCall, null)
.then(result => console.log(result)); // use your callbacks hereasync function repeatedAPICall(
apiCall,
startValue
)
const keys = [startValue];
const result = ;
for (const callKey of keys)
const data = await apiCall(callKey);
result.push(data);
if (data.key !== null) keys.push(data.key);
return result;
// mock api call
const apiCall = ((counter) => () => Promise.resolve(
key: counter < 5 ? counter++ : null
))(0);
repeatedAPICall(apiCall, null)
.then(result => console.log(result)); // use your callbacks hereasync function repeatedAPICall(
apiCall,
startValue
)
const keys = [startValue];
const result = ;
for (const callKey of keys)
const data = await apiCall(callKey);
result.push(data);
if (data.key !== null) keys.push(data.key);
return result;
// mock api call
const apiCall = ((counter) => () => Promise.resolve(
key: counter < 5 ? counter++ : null
))(0);
repeatedAPICall(apiCall, null)
.then(result => console.log(result)); // use your callbacks hereedited Nov 10 at 16:13
answered Nov 10 at 15:43
Ori Drori
72k127589
72k127589
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240489%2fmake-repeated-ajax-calls-inside-loop-until-condition-is-met%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Is this through a public api you can share link to ? Are you wanting all the data pushed into a single array ?
– charlietfl
Nov 10 at 15:45
Sorry, I can not share the API as it is not public. Yes, I want to store the data in a single array, but also update the result stored inside the array based on the response returned from every call.
– anuyog
Nov 10 at 16:27