Yas*_*apu 18 angular2-observables angular angular6
this.service.service1().subscribe( res1 => {
this.service.service1().subscribe( res2 => {
this.service.service1().subscribe( res3 => {
this.funcA(res1, res2, res3);
});
});
});
Run Code Online (Sandbox Code Playgroud)
我需要将三个数据从三个不同的API传递给一个函数.
在订阅中订阅是一种好习惯吗?
如果没有,请建议最佳方式.
Mar*_*hes 24
正确的方法是以某种方式组合各种可观察量,然后订阅整体流程 - 你如何构成它们将取决于你的确切要求.
如果你可以并行执行所有操作:
forkJoin(
this.service.service1(), this.service.service2(), this.service.service3()
).subscribe((res) => {
this.funcA(res[0], res[1], res[2]);
});
Run Code Online (Sandbox Code Playgroud)
如果每个都取决于前一个的结果:
this.service.service1().pipe(
flatMap((res1) => this.service.service2(res1)),
flatMap((res2) => this.service.service3(res2))
).subscribe((res3) => {
// Do something with res3.
});
Run Code Online (Sandbox Code Playgroud)
... 等等.有许多不同的运算符来组成可观察量.
Lia*_*iam 16
尽管上述所有内容都有助于为这个特定问题提供解决方案,但它们似乎都没有解决这里明显的根本问题,特别是:
在 subscribe 中调用 subscribe 是个好方法吗?
太长了;博士
不,在订阅中调用订阅是不好的。
为什么?
好吧,因为这不是函数式编程应该如何工作的。你不是在功能上思考,而是在程序上思考。这本身不一定是问题,但使用 rxjs (和其他反应式编程扩展)的全部目的是编写函数代码。
我不会详细介绍函数式编程是什么,但本质上函数式编程的要点是将数据视为流。由函数操作并由订阅者使用的流。一旦您在另一个订阅中添加订阅,您就可以在消费者(而不是流内)中操作数据。所以你的功能流现在被破坏了。这可以防止其他使用者在您的代码中进一步利用该流。因此,您已将功能流转变为过程。
您可以使用 forkJoin
将它们组合Observables
成一个值Observable
forkJoin(
this.service.service1(),
this.service.service2(),
this.service.service3()
).pipe(
map(([res1, res2, res3 ]) => {
this.funcA(res1, res2, res3);
})
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
11441 次 |
最近记录: |