Angular 4 - How to make bulk http syncronous requests

user3412296

I have in my Angular 4 App a formulary to upload data. This data comes in a array of JSON elements and I loop over it to make an HTTP PUT request through an observable.

If a little set of elements are going to be upload everything works fine, but sometimes input can be 1000+ elements large.

As I'm using observables, all the request are made in a synchronous way, so at the same time. That makes the backend literally crash if the number of elements is big enough.

I need to make the HTTP requests one by one but I don't know how to do it. ¿Could you help me?

Service function:

putIp(ip, data): any {
const url = `${this.IpUrl}/${ip}`;
return this.authHttp.put(url, data, {headers: this.headers});}

Component function:

       publishIps(): void {
     console.log("Publishing Ips")

     for (let _i = 0; _i < this.ipsInTable.length; _i++) {
         this.ipsInTable[_i].treated = true;
         this.ipService.putIp(this.ipsInTable[_i].id, this.ipsInTable[_i].json)
         .catch(err => {
           this.ipsInTable[_i].result = false;
           this.ipsInTable[_i].message = "Error";
           return Observable.throw(this.errorHandler(err));
         })
         .subscribe((data) => {
            // Doing some stuff
         });
     }
   }
ShaneCoder

I would tweak the backend to accept an array of objects. Then you just do one request.

If that's not an option, and you want to submit the request one at a time, here's a different approach to your controller code:

publishIp(_i: int): void {
   if (_i < this.ipsInTable.length) {

     this.ipsInTable[_i].treated = true;
     this.ipService.putIp(this.ipsInTable[_i].id, this.ipsInTable[_i].json)
     .catch(err => {
       this.ipsInTable[_i].result = false;
       this.ipsInTable[_i].message = "Error";
       return Observable.throw(this.errorHandler(err));
     })
     .subscribe((data) => {
       _i++;
       publishIp(_i);           
        // Doing some stuff
     });
  }
}
publishIps(): void {
 console.log("Publishing Ips");

 publishIp(0);     
}

Disclaimer: I just typed this into stackoverflow. You may have to fix some typos.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to make the function syncronous

How to use multiple Http Requests in Angular 4

Syncronous HTTP Angular 6 in data service construction

How to make CORS-enabled HTTP requests in Angular 2?

How to make HTTP requests in angular and get the body from the object?

How to make a sequence of http requests in Angular 6 using RxJS

Angular, how to make consecutive, dependent http get requests

Parallel HTTP requests in Angular 4

Angular 4 Http Chain Requests

How to use ForkJoin for sequence of http requests Angular 4

How to make multiple http requests?

How to perform multiple Http requests (Tasks) in bulk in Elm lang

How to make http requests using HTTP Intercepters

Angular - How to delay HTTP Requests

Can I make two separate Http get requests in same service method using angular4?

How to make http post request in Angular 4 Universal?

How to make concurrent HTTP requests with basic authentication

How do you make HTTP requests with Raku?

How to make HTTP requests using Plink

How to make HTTP client requests with Deno

How to make HTTP Requests on Flutter For Web?

How to make asynchronous HTTP requests in PHP

How to make http/https POST requests manually?

How to make http requests from COBOL

How to make multiple http requests in my case

How to make HTTP requests to an API controller with AuthorizedAttribute?

How do you make a "please wait" spinner for Angular http requests (or anything really)?

Make Syncronous call to javascript promise

Angular - How to implement switchMap for multiple http requests?