angular - Converting Array.map + Promise.all to Observable -


i have vanilla.js (typescript) http client returned promises, so:

class person {     name: string;     id: number;     friendsids: number[]; }  class myclient {      getpersoninfo(personid: number): promise<person> {          return fetch( '/people/' + personid)             .then( r => r.json() person );     } } 

my display code gets details of each person's friends, so:

refresh(): void {      client.getpersoninfo( 123 )         .then( (person: person) => {              // array-of-promises:             let friendsdetailspromises: promise<person>[] = person.friendsids.map( friendid => client.getpersoninfo( friendid  ) );              // promise-of-array:             let friendsdetailsall     : promise<person[]> = promise.all( friendsdetailspromises );              return friendsdetailsall;         } )         .then( (friendsdetails: person[]) => {              // (do stuff each person)         } )         .catch( reason => console.error( reason ) ); } 

i'm trying move on angular 2's built-in http client returns observable<t> instead of promise<t>, far have:

class myclient {      private http: httpclient;       getpersoninfo(personid: number): observable<person> {          return this.http.get<person>( '/person/' + personid );     } }  refresh(): void {      client.getpersoninfo( 123 )         .flatmap( (person: person) => {              // ...now what?         } )         .subscribe(             (friendsdetails: person[]) => {              // (do stuff each person)             },             err => console.error( err )         )     ); } 

i'm stuck on i'm supposed inside of flatmap.

i saw qa asks converting promise.all observable ( promise.all behavior rxjs observables? ) , answer use forkjoin suggests flatmap , linking posting ( rxjs promise composition (passing data) ) problem doesn't resemble mine @ all.

you can use observable.forkjoin wait sub observables complete(same promise.all).

i find below syntax works(maybe not best , wait suggestions)

this.http.get('list.json').switchmap(res => {                      return observable.forkjoin(res.map(item => this.http.get(item + '.json')));   }) 

refer plunker demo.


sorry, maybe little late took time find new usage rxjs5.


Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -