What is the best way to use observables for http requests in angular?

user11962606

I have got an Angular app that gets data from APIs. So first my code for detail.component.ts looked kind of like this:

//code
getData()
{
this.http.get(url1).subscribe(data1 => 
{/*code: apply certain filter to get a filtered array out*/

this.http.get(url2).subscribe(data2 => 
{/*code: apply certain filter to get a filtered array out*/

this.http.get(url3).subscribe(data3 => 
{/*code: apply certain filter to get a filtered array out*/
})//closing third subscribe form

})//closing second subscribe form
})//closing first subscribe form
}

As you can see, because of nesting all these calls inside each other, the more calls I will have in the future, the messier it all will get. I did some research and got the idea that observables might solve the problem. So I changed the code and this is how it looks like now - data.service.ts:

//code
getData1()
{this.data1 = this.http.get(this.url1)
return this.data1;}

getData2()
{this.data2 = this.http.get(this.url2)
return this.data2;}

getData3()
{this.data3 = this.http.get(this.url3)
return this.data3;}

detail.component.ts:

//code
ngOnInit()
{
this.dataService.getData1().subscribe(data1 => 
{/*code: apply certain filter to get a filtered array out*/

this.dataService.getData2().subscribe(data2 => 
{/*code: apply certain filter to get a filtered array out*/

this.dataService.getData3().subscribe(data3 => 
{/*code: apply certain filter to get a filtered array out*/
})//closing third subscribe form

})//closing second subscribe form
})//closing first subscribe form
}

Data1 must be executed first because Data2 and Data3 need information of the filtered Array from Data1. This is why I struggled to apply solutions like forkJoin. So my question is, if this is a good solution or if you know of a better way to make the code less messy and keep the functionality?

Jayme

You could try the async await method

async ngOnInit() {
    const data1 = await this.dataService.getData1().toPromise();
    // apply certain filter to get a filtered array out

    const data2 = await this.dataService.getData2().toPromise();
    // apply certain filter to get a filtered array out

    const data3 = await this.dataService.getData3().toPromise();
    // apply certain filter to get a filtered array out
}

You could also try merge map

this.dataService.getData1()
.pipe(
  map((data1) => {
    // code: filter data here
    return data1;
  }),
  mergeMap((data1) => {
    this.dataService.getData2();
  })
)
.subscribe((finalData) => {
});

Or concatMap

this.dataService.getData1()
  .pipe(
    tap(data1 => console.log(data1)),
    concatMap((data1) => {
      this.dataService.getData2(data1);
    }),
    tap(data2 => console.log(data2)),
    concatMap((data2) => {
      this.dataService.getData3(data2);
    }),
    tap(data3 => console.log(data3)),
  )
  .subscribe(result => {
    console.log(result)
  });

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

What is the best way to implement sequencial http requests with optimistic concurrency in angular?

What is the best way to chain observables in Angular?

What is the best way to unsubscribe infinite observables in an Angular service

What is the right way to use angular2 http requests with Django CSRF protection?

Nested Observables from nested http requests in Angular

Angular 2 Http, Observables and recursive requests

Using Observables for streams in Angular and subscribing to them in another class what is the best way to approach delivery to the DOM/template

What is the RXJS 6 way of handling http Observables?

Java best way to send multiple HTTP requests

Angular - what is the preferred way to terminate Observables?

What is the proper way to handle concatenation of data returned from multiple http observables in angular2/4 rxjs?

What is the best way to use LokiJS in my Angular 1 app?

Angular2 is this the right way to use Observables?

In Electron , what is the best way to make ajax requests?

What to use for HTTP requests in Laravel?

Angular (4): Multiple HTTP requests with Observables: one after another succeeds

What is the best professional way to detect the status of several ajax requests in Angular8?

Make synchronus http requests with Observables

What is considered the correct way to test methods that return http observables?

Angular Proper way to cache or load http requests

What is the best way of checking HTTP resources with Ansible?

What is the best way to send files through HTTP?

what is the best way of doing a http post in dart?

What is the best way to apply conditional classes in Angular? What should I use ngIf or ngClass?

Best way to fetch HTTP Requests then proccess HTML response

Best way to limit concurrent http requests in python (no threads)?

Best way for multiple HTTP Request in Angular

Angular Observables and Http

Angular 8. Unknown amount of http.get requests in array to call, must be sequential, what to use