【发布时间】:2021-07-08 20:38:08
【问题描述】:
我有两个级别的可观察对象,代码如下所示:
function level_1() {
...
level_2(params).subscribe((value) => {
if (value === something) {
action_1();
}
else { // others
action_2();
}
});
}
function level_2(params) : Observable<any> {
...
another_observable.subscribe((v) => {
if (a_condition) {
return of(something); // this is reached, but the observable is not captured
}
else {
return of (others);
}
});
return of(others); // this is captured
}
现在的问题是,在三个“返回”中,level_1中只有第三个被捕获,而level_2中达到了第一个,而level_1中没有捕获到。 我虽然 observable 会继续听,但有什么我遗漏的吗?
【问题讨论】:
-
什么是
others。为什么你认为return of(something)应该在level_2中返回一些东西?那是完全不同的功能。他们不必互相做任何事情...... -
我们可以认为这里有些东西是“真”,而另一些是“假”。我的问题是,为什么 level_1 函数中没有捕捉到 return of(true)。
-
为什么你认为它应该。
another_observable.subscribe确实与level_2返回的 observable 无关 -
所以无论如何我都可以让 another_observable 发出可以被 level_1 捕获的东西: level_2(params).subscribe((value) {}
标签: javascript rxjs observable subscribe