Ayoush Kohli
Ayoush Kohli

Reputation: 35

How to call a method when all the http request are completed in Angular 2

I want to check the checkbox when my data and the filter data request are completed. How can I check whether my all the requests are completed and then call that method.

Upvotes: 0

Views: 361

Answers (1)

First have a look at RxJs' Observable.forkjoin and how Observables work.

Here is some sample code that should get you started :

const urls = ['myUrl1', 'myUrl2'];

const requestArray = makeMultipleRequest(this.observableArr(urls));

observableArr = queryUrls => queryUrls.map((url) => this.httpRequest(url));
makeMultipleRequest = (requestArr) => Observable.forkJoin(requestArr);        

httpRequest = (url) => this.http.get(url).map((res) => res.json())
                                         .catch((err) => handleError())

requestArray.subscribe((data) => {
  // Do what you want with the results from your requests
});

Don't forget to import { Observable } from 'rxjs/Rx'; And import { Http } from '@angular/http';

Good luck

Upvotes: 0

Related Questions