[英]forkJoin but next observable depends on the one before
https://www.learnrxjs.io/learn-rxjs/operators/combination/forkjoin https://www.learnrxjs.io/learn-rxjs/operators/combination/forkjoin
const example = forkJoin({
// emit 'Hello' immediately
sourceOne: of('Hello'),
// emit 'World' after 1 second
sourceTwo: of('World').pipe(delay(1000)),
// throw error
sourceThree: throwError('This will error')
}).pipe(catchError(error => of(error)));
// output: 'This will Error'
const subscribe = example.subscribe(val => console.log(val));
That's the main implementation but in my case, to call sourceTwo
I need to use the data from sourceOne
and the same with sourceThree
.这是主要的实现,但在我的情况下,要调用sourceTwo
我需要使用来自sourceOne
的数据,并且与sourceThree
相同。 All calls need the previous observable in order to fetch data.所有调用都需要先前的 observable 才能获取数据。
I only care about final result, don't need to merge anything, just do what this example does, show what sourceThree
returns我只关心最终结果,不需要合并任何东西,只做这个例子所做的,展示sourceThree
返回的内容
forkJoin is intended to run calls in parallel , like Promise.all()
. forkJoin 旨在并行运行调用,如Promise.all()
。 In your case, you cannot do that since the calls depend on each other.在您的情况下,您不能这样做,因为调用相互依赖。
Instead, you can pipe them .相反,您可以通过管道传输它们。 If you can parallelize 2 and 3, you can do something like the following.如果可以并行化 2 和 3,则可以执行以下操作。
sourceOne.pipe(
res => forkJoin(makeCall2(res), makeCall3(res))
)
To complete the Promise comparison:要完成 Promise 比较:
makeCall().then(res => Promise.all([
makeCall2(res),
makeCall3(res)
]));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.