How do I return the results of multiple asynchronous calls in Angular service

vincentf

In AngularJS, I can use return $q.all(promises) to return a promise to controllers. What's the right way to do it in Angular? How can I return data to components ?

My Service:

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

import { Item } from '../item';

@Injectable()
export class GetItemListService {
  constructor(private http: Http) { }

  private url1 = 'urlToGetItemList1';
  private url2 = 'urlToGetItemList2';

  getItemList():  ??? <Item[]> {
    Observable
        .forkJoin(
            this.http.get(url1).map(res => res.json()),
            this.http.get(url2).map(res => res.json())
        )
        .subscribe(
            data => {
                // this is the result I want to return to component
                return data
            }
        )
  }
}
vincentf

Resolved it with @echonax's answer. Return Observable.forkJoin and subscribe it in component.

Service:

getItemList():  Observable <Item[]> {
    return Observable
        .forkJoin(
            this.http.get(url1).map(res => res.json()),
            this.http.get(url2).map(res => res.json())
        )
  }

Component:

ngOnInit(): void {
      this.getItemListService.getItemList()
        .subscribe(data => {
            console.log(data)
        })
  }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I return the accumulated results of multiple (parallel) asynchronous function calls in a loop?

From Dev

How do I return the accumulated results of multiple (parallel) asynchronous function calls in a loop?

From Dev

How to return the response of multiple asynchronous calls?

From Dev

How do I make asynchronous calls in an event handler

From Dev

How do I handle multiple browser scripts making the same calls to the back-end service

From Dev

How do I handle multiple browser scripts making the same calls to the back-end service

From Dev

How do I create a naturally asynchronous method when inside calls are not naturally asynchronous?

From Dev

How do I return the results of this function?

From Dev

How do I return a list of results with rxjs

From Dev

Return Asynchronous Data from Angular 2 Service

From Java

How do I return the response from an asynchronous call?

From Dev

How do I return strings from an asynchronous POST request?

From Dev

Swift: How do I return a value within an asynchronous urlsession function?

From Dev

How do I return the TaskStatus when making a synchronous call asynchronous?

From Dev

How do I return multiple results from a SQL Server Stored Procedure with PHP prepared statements?

From Dev

How do I return the success value of chaining multiple Results if no errors occur or another value if any error happens?

From Dev

Angular 2 How do I call a service method when an observable changes/when a services returns new results?

From Dev

How to use asynchronous calls in a loop in angular?

From Dev

How to use asynchronous calls in a loop in angular?

From Dev

How do you test Angular 2 directive that calls a service?

From Dev

Best way to make multiple asynchronous calls to same web service

From Dev

How to do Unit Testing for asynchronous calls?

From Dev

Unit testing multiple asynchronous calls that return promises with Mocha

From Dev

Unit testing multiple asynchronous calls that return promises with Mocha

From Dev

How do I make web service calls within nginx?

From Dev

Typescript Angular 2 How do I return JSON data from a service to a component?

From Dev

How do I run asynchronous functions in order in Angular?

From Dev

How do I return different values on different calls to a mock?

From Dev

Android multiple asynchronous calls

Related Related

  1. 1

    How do I return the accumulated results of multiple (parallel) asynchronous function calls in a loop?

  2. 2

    How do I return the accumulated results of multiple (parallel) asynchronous function calls in a loop?

  3. 3

    How to return the response of multiple asynchronous calls?

  4. 4

    How do I make asynchronous calls in an event handler

  5. 5

    How do I handle multiple browser scripts making the same calls to the back-end service

  6. 6

    How do I handle multiple browser scripts making the same calls to the back-end service

  7. 7

    How do I create a naturally asynchronous method when inside calls are not naturally asynchronous?

  8. 8

    How do I return the results of this function?

  9. 9

    How do I return a list of results with rxjs

  10. 10

    Return Asynchronous Data from Angular 2 Service

  11. 11

    How do I return the response from an asynchronous call?

  12. 12

    How do I return strings from an asynchronous POST request?

  13. 13

    Swift: How do I return a value within an asynchronous urlsession function?

  14. 14

    How do I return the TaskStatus when making a synchronous call asynchronous?

  15. 15

    How do I return multiple results from a SQL Server Stored Procedure with PHP prepared statements?

  16. 16

    How do I return the success value of chaining multiple Results if no errors occur or another value if any error happens?

  17. 17

    Angular 2 How do I call a service method when an observable changes/when a services returns new results?

  18. 18

    How to use asynchronous calls in a loop in angular?

  19. 19

    How to use asynchronous calls in a loop in angular?

  20. 20

    How do you test Angular 2 directive that calls a service?

  21. 21

    Best way to make multiple asynchronous calls to same web service

  22. 22

    How to do Unit Testing for asynchronous calls?

  23. 23

    Unit testing multiple asynchronous calls that return promises with Mocha

  24. 24

    Unit testing multiple asynchronous calls that return promises with Mocha

  25. 25

    How do I make web service calls within nginx?

  26. 26

    Typescript Angular 2 How do I return JSON data from a service to a component?

  27. 27

    How do I run asynchronous functions in order in Angular?

  28. 28

    How do I return different values on different calls to a mock?

  29. 29

    Android multiple asynchronous calls

HotTag

Archive