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. JavaScript--Date 日期对象

    日期对象可以储存任意一个日期,并且可以精确到毫秒数(1/1000 秒). 定义一个时间对象 : var Udate=new Date(); 注意:使用关键字new,Date()的首字母必须大写. 使 ...

  2. 【百度之星】-IP聚合

    问题描述: Problem Description 当今世界,网络已经无处不在了,小度熊由于犯了错误,当上了度度公司的网络管理员,他手上有大量的 IP列表,小度熊想知道在某个固定的子网掩码下,有多少个 ...

  3. CDH5.7Hadoop集群搭建(离线版)

    用了一周多的时间终于把CDH版Hadoop部署在了测试环境(部分组件未安装成功),本文将就这个部署过程做个总结. 一.Hadoop版本选择. Hadoop大致可分为Apache Hadoop和第三方发 ...

  4. MVC系列学习(十三)-合并JS和CSS

    1.先来看看,不用合并js的情况,传输量大 1.1新建一个 [基本]的mvc项目 然后新建一个控制器HomeController,因为js会在很多视图中用到,所以此时我们添加一个视图带布局页Index ...

  5. body全屏css/网页全屏设置/全屏样式

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  6. fcc html5 css 练习2

    <form action="/submit-cat-photo" >action属性的值指定了表单提交到服务器的地址 <input type="text ...

  7. JS高级——沙箱

    基本概念 1.沙箱:与外界隔绝的一个环境,外界无法修改该环境内任何信息,沙箱内的东西单独属于一个世界 2.苹果手的app使用的就是沙箱模式去运行,隔离app的空间,每个app独立运行 js沙箱基本模式 ...

  8. [Windows Server 2008] 安装Apache+PHP+MySQL

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频.★ 本节我们将带领大家:Win2008 ...

  9. Linux内存压力测试-memtester工具

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

  10. Eclipse中自动生成get/set时携带注释给get/set

    Eclipse中自动生成get/set时携带注释给get/set   编码的时候通常要用到 JavaBean ,而在我们经常把注释写在字段上面,但生成的Get/Set方法不会生成,通过修改Eclips ...