0

Hi StackOverflow community members,

I am working on Angular 8. I have a question on how to breakdown my HTTP request, instead of sending 1000 requests in 1 go, I would like to have something like sending every 50 requests at 1 time only. I couldn't think of any way of achieving this. Below is my code snippets:

for (let i = 0; i < products.length; i++) {
    this.myService.getProductInfoByCountryAndProductId(this.selectedCountries, this.product[i])
    .subscribe(
       (res) => { //get result },
       (error) => { //throw error },
       () => { //do something here });
}

The code above would triggers all the requests in 1 go, imagine if I have 1000 products in products array, meaning it would loop 1000 times, and 1000 requests would sent to the server and caused performance issue. Is there any way to limit the client not to send all request in one go?

I've tried combineLatest, the outcome isn't what I want. I wanted to try concatMap but meaning it would send HTTP request 1 by 1 and going to take a long time to process all the products. Any suggestions or ideas?

Chuah Cheng Jun
  • 243
  • 1
  • 3
  • 17
  • 1
    Do you really need to populate all of that info up front? Not sure about your application, but normally I'd expect to use pagination and only load what I need. – stjns Mar 06 '20 at 01:57
  • @Bodacious This is not to display data but to gather all data and export all these into Excel. My client wants it to be able to export all products (about 1400+ so far in the DB). I've told them they might have consequences on performance issue but they insisted. – Chuah Cheng Jun Mar 06 '20 at 02:01
  • Gotcha. And I guess it's not possible to implement an endpoint on the server side to return everything? – stjns Mar 06 '20 at 02:06
  • @Bodacious I proposed on doing that, populating all the data on server side and send an email with the Excel attachment to the end user, but still doesn't get any respond from client side yet. So I am thinking to search for any answer in SO first, if this can be done, so I don't need to waste my time on writing new implementation in server side. – Chuah Cheng Jun Mar 06 '20 at 02:10
  • It is possible, sure, but you're still looking at 1000 http requests even if they're batched. You don't have to send an email from the sever, you just need an endpoint that can return info for more than one product at a time. – stjns Mar 06 '20 at 02:12

1 Answers1

0

The problem you are trying to solve is more about architecture than angular http capabilities.

At the end, the number of bytes exchanged via HTTP will be the same. You now have to figure out 'how'.

If you want to reduce the number of API calls, a solution can be an adjustment of your webservice to accept more than one product id, and the frontend ready to receive an array instead of an individual result.

glihm
  • 1,138
  • 13
  • 29