mahmoudismail mahmoudismail - 8 months ago 47
HTTP Question

Multiple $http requests in Ionic2

I want to know whether the Multiple request :

if my

$http request 1
start , let's say
$http request 1
ends and tried to call
$http request 2
. my question how can i create a multiple request ?

for example : call
$http request 1
$http request 2


As I understand, you're trying to make more than one http request and then process the response when all those request have ended. For instance, you may need to load data from more than one source, and delay the post-loading logic until all the data has loaded.

If that's the case, you could use ReactiveX Observables because it provides a method called forkJoin() to wrap multiple Observables.

import {Injectable} from '@angular/core';
import {Http, Response} from '@angular/http';
import {Observable} from 'rxjs/Rx';

export class MultipleHttpService {

  constructor(private http:Http) { }

  // If any single request fails, the entire operation will result in an error state.
  getData0AndData1() {
    return Observable.forkJoin(
      this.http.get('/app/data0.json').map((res:Response) => res.json()),
      this.http.get('/app/data1.json').map((res:Response) => res.json())


Then you could get all the data by subscribing to that observable:

// Code in your page ...
      data => {
        this.data0= data[0]
        this.data1 = data[1]
      err => console.error(err)

Working plunker