1

I'm working on a angular cli project , i have a method that evaluates multiple https request .`

   this.files = this.http.get("assets/L10n/customer.json")
                        .map(res => res.json())
                        .subscribe(data => console.log(data));
    } 
     this.files2 = this.http.get("assets/L10n/customer2.json")
                        .map(res => res.json())
                        .subscribe(data => console.log(data));
    }  

I want to return this.files and this.files2 as an array .Any help is appreciated`

dockerrrr
  • 277
  • 1
  • 5
  • 17

1 Answers1

0

You can use forkJoin:

let first = this.http.get("assets/L10n/customer.json")
    .map(res => res.json());
let second = this.http.get("assets/L10n/customer2.json")
    .map(res => res.json());

Observable.forkJoin(first, second).subscribe(
    (resultArray) => {
        // `resultArray` is a combined array of first and second results
        console.log(resultArray);
    }
)
Saravana
  • 37,852
  • 18
  • 100
  • 108
  • Thanks it worked .console im getting the value .But when i try to use the value in another method its giving undefined – dockerrrr Sep 04 '17 at 03:08
  • instead of console i added this.result=data .And when i try to print this.result in another method its saying undefined – dockerrrr Sep 04 '17 at 03:10
  • Are you accessing `this.data`? Remember these actions are asynchronous. You will get the value only after the HTTP calls are completed -- which are asynchronous. – Saravana Sep 04 '17 at 03:12
  • OK.So what i have to do – dockerrrr Sep 04 '17 at 03:14
  • You can use [`async/await` on the observables](https://stackoverflow.com/questions/34190375/how-can-i-await-on-an-rx-observable). Or pass the function where you want the data as callback and execute it inside `subscribe`. – Saravana Sep 04 '17 at 03:20
  • Can you edit your question with how your code is structured? – Saravana Sep 04 '17 at 06:16