Angular rxjs Observable.forkJoin 404

Angular rxjs Observable.forkJoin 404

我需要调用几个不同的服务来构建用户对象。其中一个调用并不总是 return 数据,因为数据是可选的。

我有下面的代码,但是当使用可选的 API 调用遇到 404 时,代码永远不会进入 .map 函数。我看到它在可选的 URL API 调用中遇到了问题,但是 .map 从未被调用过。是否可以将 forkJoin 与可能 return 404 响应的 API 一起使用?

    return Observable.forkJoin([
        this.wmrk_http.get('required-url')
            .map((res:Response) => <user> res.json())
            .catch((res:Response) =>  Observable.empty<user>()),
        this.wmrk_http.get('required-url-2')
            .map((res:Response) => <groups> res.json())
            .catch((res:Response) =>  Observable.empty<groups>()),
        this.wmrk_http.get('optional-data-url')
            .map((res:Response) => <userData> res.json())
            .catch((res:Response) => Observable.empty<userData>()),     
    ])
    .map((data: any[]) => {
        ...
    });

forkJoin stops the chain if one of the items doesn't emit a value. So use either use defaultIfEmpty or replace observable.empty with observable.of(null). See the working plunker

    Observable.forkJoin([
        this.http.get('https://api.github.com/users/karser')
            .map((res:Response) => res.json())
            .catch((res:Response) =>  Observable.of(null)),
        this.http.get('https://api.github.com/fsdfsdf')
            .map((res:Response) => res.json())
            .catch((res:Response) =>  Observable.of(null)),
        this.http.get('https://api.github.com/2')
            .map((res:Response) => res.json())
            .catch((res:Response) => Observable.of(null))
    ])
    .map((data: any[]) => {
        console.log(data)
    })
    .subscribe();

输出为

GET https://api.github.com/users/karser 200 (OK)
GET https://api.github.com/fsdfsdf 404 (Not Found)
GET https://api.github.com/2 404 (Not Found)

[Object, null, null]