We can use Subject as Observable and Observer:

// Subject should be only used to emit value for private
// Using subject as an Observer
const subject = new Subject(); subject.next(1);
subject.next(2);
subject.next(3); // Using subject as an Observable
const source$ = subject.asObservable(); source$.subscribe(console.log);

AsyncSubject:

AsyncSubject only subscribe the latest value before subject complete.

const as = new AsyncSubject();

as.next(1);
as.next(2);
as.next(3);
as.complete(); // it is necessary to complete it in order to get last value const source$ = as.asObservable(); source$.subscribe(console.log); //

If we don't call 'as.complete()', we won't get any value in the console.

Different from AsyncSubject, we have ReplySubject:

It doesn't need to call complete() in order to emit value. And it emit all the values from every beginning.

const rs = new ReplaySubject();

rs.next(1);
rs.next(2);
rs.next(3); const source$ = as.asObservable(); source$.subscribe(console.log); // 1,2,3

[RxJS] AsyncSubject and ReplaySubject - Learn the Differences的更多相关文章

  1. [RxJS] AsyncSubject: representing a computation that yields a final value

    This lesson will teach you about AsyncSubject, another type of Subject with some replaying logic ins ...

  2. [RxJS] AsyncSubject

    AsyncSubject emit the last value of a sequence only if the sequence completed. This value is then ca ...

  3. RxJS之AsyncSubject

    AsyncSubject 是另一个 Subject 变体,只有当 Observable 执行完成时(执行 complete()),它才会将执行的最后一个值发送给观察者. import { Compon ...

  4. What are the differences between struct and class in C++?

    Question: This question was already asked in the context of C#/.Net. Now I'd like to learn the diffe ...

  5. Angular全局数据管理与同步更新

    自定义实现angular中数据的状态管理,如有不妥请指正 一.先介绍一下rxjs中subject: Import {subject}from’rxjs’ Subject 数据的订阅与分发,结合报刊的发 ...

  6. Difference between ref and out parameters

    Original link: http://www.dotnet-tricks.com/Tutorial/csharp/K0Hb060414-Difference-between-ref-and-ou ...

  7. Using FireMonkey Layouts

    FireMonkey has many layout controls to choose from. Come learn the differences and how to use them t ...

  8. OrientDB入门(1)Getting Started

    Running OrientDB the First Time First, download and extract OrientDB by selecting the appropriate pa ...

  9. 学习笔记:Rick's RoTs -- Rules of Thumb for MySQL

    Table of Contents SELECTs -- do's and don'tsINDEXingENGINE DifferencesOptimizations, and notPARTITIO ...

随机推荐

  1. scrapy安装及基本使用

    前端html, css, js 相关知识 数据库运用 http协议的了解 前后台联动 蜘蛛中间件.下载中间件 下载中间件的地方可以写各种反爬的策略 1.使用pip安装, pip3 install sc ...

  2. 自定义View(9)使用Renderscript 渲染特效。

    1.渲染脚本官网 https://developer.android.com/guide/topics/renderscript/compute 2.高斯模糊 ScriptIntrinsicBlur ...

  3. strcpy 和 memcpy自实现

    都是套路,详见代码注释: #include <stdio.h> #include <assert.h> #include <iostream> using name ...

  4. [转]Linux命令之iconv

    转自:http://lorna8023.blog.51cto.com/777608/420313 用途说明 iconv命令是用来转换文件的编码方式的(Convert encoding of given ...

  5. vb.net实现textbox控件输入指定位数小数方法实现。

    Private Sub textbox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPres ...

  6. Web页面使用VLC播放插件

    一.原生态Demo下载 选择原因:我们为什么选择VLC播放插件?原因是它支持IE8浏览器播放视频,如果高版本的浏览器大可不必选择该插件,很多html5插件既好用又简单,但是有些交管或政府 部门还是限制 ...

  7. 对“空引用”说bye-bye

    大家可能经常遇到这种情况:当一个对象为null时,调用这个对象的方法或者属性时,就会报错:“Object reference not set to an instance of an object.” ...

  8. ie9以下的浏览器兼容性问题

    .bind不兼容的问题Function.prototype.bind = function () { var fn = this, args = Array.prototype.slice.call( ...

  9. [转]STL之list容器详解

    List 容器 list是C++标准模版库(STL,Standard Template Library)中的部分内容.实际上,list容器就是一个双向链表,可以高效地进行插入删除元素. 使用list容 ...

  10. Redis主从复制失败(master_link_status:down)

    今天配置redis主从复制时出现master_link_status:down提示. 首先打开slave的redis.conf配置文件,确定slaveof 和masterauth 两个选项配置是否正确 ...