do(tap)vs subscribe [英] do (tap) vs subscribe

查看:104
本文介绍了do(tap)vs subscribe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:在RxJs 6之前,点击被称为 do 。更新的标题也反映了点击。

Prior to RxJs 6, tap was called as do . Updated title to reflect tap too.

我想了解使用 .subscribe 的最佳做法。执行 Observables的方法。

I would like to understand what is the best practice of using .subscribe and .do methods of Observables.

例如,如果我需要在从服务器加载初始数据后做一些工作

For example if I need to do some job after initial data is loaded from server

const init$: Observable<MyData> = this._dataService.getData();

init$
  .do((initialData: MyData) => {
    this.data = initialData; // at this step I am initializing the view
  })
  .switchMap(() => loadExtraData)
  .subscribe((extraData) => {
     doSomething(extraData, this._data); // I need this._data here
  }); 

我可以用 .subscribe 做同样的事情

const init$: Observable<MyData> = this._dataService.getData()
  .shareReplay(1);

init$
  .subscribe((initialData: MyData) => {
    this.data = initialData; // at this step I am initializing the view
  })

init$
  .combineLatest(loadExtraData)
  .subscribe(([initialData, extraData]) => {
     doSomething(extraData, initialData); // I need this._data here
  }); 

哪一个更好,为什么?

推荐答案

编辑:对于RxJS 6或更高版本,请阅读执行作为点击

For RxJS 6 or above, Read do as tap.


do 用于副作用。 subscribe 用于调用observable。用 subscribe 替换 do 会产生不希望的结果。用 do 替换 subscribe 甚至不会调用流。

do is used for side-effects. subscribe is used to invoke an observable. Replacing do with subscribe creates undesired results. Replacing subscribe with do will not even invoke the stream.

请考虑以下示例:

使用订阅

const testObservable = Rx.Observable.create(function(observer){
   console.log('some heavy task, may be network call ');
   observer.next('success');
});

testObservable.subscribe(function(res){
    console.log('action 1');
});

testObservable.subscribe(function(res){
   console.log('action 2');
});

上述代码的输出是

"some heavy task, may be network call "
"action 1"
"some heavy task, may be network call "
"action 2"

你可以看到 Rx.Observable.create 执行了两次。我们的目标是只做一次,但与行动2一起,也行动1

You can see the Rx.Observable.create got executed twice. Our goal is do it only once but along with action 2, do action 1 also.

使用执行

const testObservable = Rx.Observable.create(function(observer){
   console.log('some heavy task, may be network call ');
   observer.next('success');
});

testObservable
    .do(function(res){
        console.log('action 1');
    })  
    .subscribe(function(res){
        console.log('action 2');
    });

输出

"some heavy task, may be network call "
"action 1"
"action 2"

这就是我们真正想要的。我们需要'行动2'但在此之前也需要'行动1'。

This is what we actually wanted. We need 'action 2' but before that do 'action 1' also.

为什么称为副作用

因为它不会影响与其他运营商不同,流的流动。它接受响应,做某事,即使它修改了流将忽略它的响应。例如:

Because it will not affect the flow of stream unlike other operators. It takes the response , does something and even if it modifies the response the stream is going to ignore it . For ex:

testObservable
    .do(function(res){
        console.log('action 1');
        return res+'some other text';
    })  
    .subscribe(function(res){
        console.log('action 1');
    });

上述代码仍会提供与以前相同的输出。所以无论你在中执行什么,流都会忽略它并继续执行它。

The above code will still give the same output as before. So no matter what you execute in do the stream is going to ignore it and proceed with its execution.


如果我们正在进行纯粹的功能反应式编程,我们不希望流中出现任何副作用。因此,不鼓励 do ,并且主要仅用于调试目的。

If we are doing pure 'functional reactive programming' we don't want any side effects in the stream. So, do is discouraged and mostly used only for debugging purposes .

这篇关于do(tap)vs subscribe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆