I have tried it all. I want to fetch from my server some data in a loop, and add the ressponses in an array in the order I run the for loop.
I am thinking the answer is in recursion, but tried it several times and failed.
I am using AXIOS:
Maybe that code is completely wrong, it is anyway like the 7th time I write it… I tried with promises, async/await, sync, and now recursion…
You can run the axios calls in sequence (one after the other) like this:
Or, you can run them in parallel and collect the results in order:
If you run them in parallel (which may finish sooner), you have to make sure that the target server will accept however many simultaneous requests as your array is long. Some servers will limit or reject too many simultaneous requests. To run semi-in-parallel where you have N requests in flight at the same time (how you would process a large array in parallel without too many requests in flight at once), you would use something like mapConcurrent()
to control the execution.
If you have better answer, please add a comment about this, thank you!