Hi there,
I'm pulling some data from the Store Leads API, using the page parameter set as "-1", which basically does this:
When using page=-1, results are generated asynchronously and the the initial request will immediately return an HTTP 202 response. The client should poll the same url, approximately once every 5 seconds, until an HTTP 200 response is returned (along with results).
Right now I'm running it manually until the results are retrieved. Is there a way I could automatically have this endpoint polled every few seconds until it retrieves the HTTP 200 response?
Hey @grefosco, welcome to the community 
Very high level: you could use a JS query that hits the API, evaluates if it's a 200
response and if not just runs it again.
Be sure to wrap your code in (async () => {
and use the await
and resolve
functions. Something like this:
var leads = await new Promise((resolve) => {
get_leads.trigger({
additionalScope: {
someData: someData
},
onSuccess: (data) => {
resolve(data);
}
});
});
the leads
variable now holds your response. From there you can build a loop until 200
is returned, etc.
Does that make sense?
1 Like
Hey @minijohn , thanks for your reply!
In theory I believe I understood your suggestion, but as I'm not well versed in JS, I couldn't figure much what to do with it, but with the help of a team mate, we came up with this, which worked perfectly:
(async function fetchDomainApps() {
let response = await domainsApps.trigger();
while (response.message) {
setTimeout(async () => {
response = await domainsApps.trigger();
}, 5000);
}
})();
We couldn't figure out how to read the status of the request, hence we used response.message
instead.
Thanks for your help!
1 Like
I am doing the same thing here but my query just runs infinitely and I have to close my browser.
(async function fetchAirflowSuccess() {
let response = await qryGetDagSuccess.trigger();
console.log(response)
while (response != "success") {
console.log(response)
setTimeout(async () => {
response = await qryGetDagSuccess.trigger();
}, 5000);
}
})();
Hey @fowlerjk, await qryGetDagSuccess.trigger()
will evaluate to the data property of qryGetDagSuccess
. With that in mind, I would expect response != "success"
to always be true, and for your loop to run forever.
Does
(async function fetchAirflowSuccess() {let response = await qryGetDagSuccess.trigger();console.log(response)while (!response) {console.log(response)setTimeout(async () => {response = await qryGetDagSuccess.trigger(); }, 5000);}})();
work on your end? If not, could you share what response looks in your console.logs?