[英]How do I make an observable depend on another observable
I have a problem understanding the order of events when building 2 observables that depend on the same underlying source.在构建依赖于相同基础源的 2 个可观察对象时,我无法理解事件的顺序。 I hope you can not only help me with a working solution, but also explain why I get the outcome below.我希望您不仅可以帮助我找到可行的解决方案,还可以解释为什么我会在下面得到结果。 My goal is that observable2
never emits before observable1
.我的目标是observable2
永远不会在observable1
之前发出。
Code代码
const filters$ = new Subject();
const observable1 = filters$.pipe(
map(() => 'obersvable1')
);
const observable2 = observable1.pipe(
map(() => 'observable2')
)
observable2.subscribe((v) => console.log(v));
observable1.subscribe((v) => console.log(v));
Expected outcome预期结果
observable1
observable2
Actual outcome实际结果
observable2
observable1
The problem is that when the filters$
subject emits, observable2
emits first?问题是当filters$
主题发出时, observable2
先发出? ♂️ ♂️
I have tried using the mergeMap
operator on observable2
, to make it "depend" on observable1
--> but to no help at all.我尝试在observable2
上使用mergeMap
运算符,使其“依赖”于observable1
--> 但根本没有帮助。
Reproduce复制
Here is a link to a stackblitz with typescript and rxjs.这是一个带有 typescript 和 rxjs 的 stackblitz 的链接。
You can make the emissions from observable1
asynchronous before passing them to observable2
:您可以在将observable2
observable1
使它们异步:
const observable2 = observable1.pipe(
observeOn(asyncScheduler),
map(() => 'observable2')
);
Your updated demo: https://stackblitz.com/edit/rxjs-xqcyqk?file=index.ts您更新的演示: https://stackblitz.com/edit/rxjs-xqcyqk?file=index.ts
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.