4

Apologies if the title is a little obtuse, this is a hard one to condense into a single sentence.

I'm using axios to request a resultset from an HTTP endpoint. This endpoint can only deliver 50 records at a time, but will have an unknown total number of records. What I'd normally do is simply while my way through, incrementing the "startAt" parameter, and break out of the loop when no more results are returned.

I can't do that here, though, as I cannot break out of the loop while in the axios callback function (which I need to be using to determine whether I have any results returned).

I feel like this is a "can't see the forest for the trees" problem, but can anyone suggest the best way out of this?

Tibrogargan
  • 4,508
  • 3
  • 19
  • 38
rjcoupe
  • 123
  • 5
  • Could you not use the length attribute of the returned object? – nagrom97 Jan 09 '17 at 09:45
  • @morganAbbotts - no, the length will only ever be up to 50, regardless of how large the (total) resultset is as the endpoint will only return 50 records at a time. What I need to do is specify and increment the startAt parameter to retrieve the next 50, then the next 50, etc etc until no further records exist. – rjcoupe Jan 09 '17 at 09:46
  • Can you not save initial length and use it as `start-point` for next iteration? So you will never need to break the loop. Just check the `start-value` > `array.length` and process the newly received records – Rajesh Jan 09 '17 at 09:50
  • 1
    It seems that recursion is what you looking for. – hindmost Jan 09 '17 at 09:50
  • You need to post some code to show what you're doing. At first glance this could be solved by having a global "flag" variable (or some other client side stoirage mechanism, cookie maybe) that is set in the callback when it runs out of records and is tested before the next request is made. – Tibrogargan May 01 '17 at 20:07

0 Answers0