简体   繁体   English

将Array.map + Promise.all转换为Observable

[英]Converting Array.map + Promise.all to Observable

I have Vanilla.js (TypeScript) HTTP client that returned Promises, like so: 我有Vanilla.js(TypeScript)HTTP客户端,它返回Promises,如下所示:

class Person {
    name: string;
    id: number;
    friendsIds: number[];
}

class MyClient {

    getPersonInfo(personId: number): Promise<Person> {

        return fetch( '/people/' + personId)
            .then( r => r.json() as Person );
    }
}

My display code gets the details of each Person's friends, like 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 for each Person)
        } )
        .catch( reason => console.error( reason ) );
}

I'm trying to move this over to Angular 2's built-in HTTP client which returns Observable<T> instead of Promise<T> , so far I have: 我正在尝试将其移至Angular 2的内置HTTP客户端,该客户端返回Observable<T>而不是Promise<T> ,到目前为止,我有:

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 for each Person)
            },
            err => console.error( err )
        )
    );
}

I'm stuck on what I'm supposed to do inside of flatMap . 我被困在flatMap内部应该做的事情上。

I saw this QA which asks about converting Promise.all to Observable ( Promise.all behavior with RxJS Observables? ) and the answer is to use forkJoin but also suggests flatMap and linking to this posting ( RxJS Promise Composition (passing data) ) but this problem doesn't resemble mine at all. 我看到了这个质量检查,询问如何将Promise.all转换为ObservableRxJS Observable Promise.all行为? ),答案是使用forkJoin但也建议flatMap并链接到此发布( RxJS Promise Composition(传递数据) ),但这问题一点都不像我的。

You can use Observable.forkJoin which will wait for all sub observables to complete(same as Promise.all). 您可以使用Observable.forkJoin ,它将等待所有子可观察项完成(与Promise.all相同)。

I find the below syntax which works( Maybe not the best and wait for suggestions ) 我发现以下语法有效( 可能不是最好的并等待建议

this.http.get('list.json').switchMap(res => {                   
  return Observable.forkJoin(res.map(item => this.http.get(item + '.json')));  
})

Refer this Plunker demo . 请参阅此Plunker演示


Sorry, Maybe this is a little late for it took some time to find new usage about rxjs5. 抱歉,也许这有点晚了,因为它花了一些时间来找到有关rxjs5的新用法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM