Welcome to JiKe DevOps Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
540 views
in Technique[技术] by (71.8m points)

rxjs forkjoin问题

问题描述

使用rxjs的forkjon想要处理两个异步请求的结果,但是其中一个异步请求可能会因为某种原因不需要发送。因此我会返回一个空的流,但是这会导致另一个正常想要发送的请求自动cancel,请问有什么解决方案

问题出现的环境背景及自己尝试过哪些方法

相关代码

粘贴代码文本(请勿用截图)

export class SystemService {
    constructor(private http: HttpClient) {}
    
    public getSystemInfo (): Observable<SystemInfo> {
        return this.http.get<SystemInfo>('/goods/test',{withCredentials: true})
    } 

    public factory() {
        const source$ = this.getSystemInfo();
        const source$2 = of(1);
        return of('').pipe(mergeMap(([source$, source$2])=> {
            return forkJoin((source, result)=> {
                return source;
            })
        }))
    }

}

在component.ts中调用

this.sysService.factory().subscribe(res=> {
            console.log(res);
        })

你期待的结果是什么?实际看到的错误信息又是什么?

期待返回正常调用的请求的结果即source$的结果,但是这种写法不报错,也不会下发getSystemInfo()的请求。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to JiKe DevOps Community for programmer and developer-Open, Learning and Share
...