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. DFS之城堡问题

    2019-06-01 17:54:51 坚持!! 题目链接: http://bailian.openjudge.cn/practice/2815 #include <bits/stdc++.h& ...

  2. printf的字符型

    参  数 说  明 %s 按实际宽度输出一个字符串 %ms m指定宽度(不足时左补空格,大于时按实际宽度输出) %-ms 左对齐,不足时右补空格 %m.ns 输出占m个字符位置,其中字符数最多n个,左 ...

  3. 给网站添加运行时间的JavaScript完整代码

    function secondToDate(second) { if (!second) { return 0; } var time = new Array(0, 0, 0, 0, 0); if ( ...

  4. RecyclerView中item无法充满的问题

    首先致谢:https://blog.csdn.net/yuanlvmao/article/details/51694211 咱们不是代码的生产者,只是代码的搬运工. 今天写了一个RecyclerVie ...

  5. Mac OS 小知识

         删除Mac OS输入法中自动记忆的用户词组 有时候不小心制造了一个错误的词组,结果也被输入法牢牢记住,这时候可以用shift+delete组合键来删除      快捷键拾遗 Fn+Delet ...

  6. @RequestMapping参数value和params的区别

    value的值可以是一个url地址的形式或者正则表达式或者rest风格的形式,而params正如其名是参数,访问的时候params的值只能作为参数,不能作为访问的地址,请看下例> value的使 ...

  7. PHP 之递归遍历目录与删除

    /** * @Description: 递归查询目录文件 * @Author: Yang * @param $path * @param int $level * @return array */ f ...

  8. POJ1161——The Suspects

    POJ1161——The Suspects   The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 48 ...

  9. js事件委托或事件代理

    起因: 1.这是前端面试的经典题型,要去找工作的小伙伴看看还是有帮助的: 2.其实我一直都没弄明白,写这个一是为了备忘,二是给其他的知其然不知其所以然的小伙伴们以参考: 概述: 那什么叫事件委托呢?它 ...

  10. ListView学习(一)

    最近了解了LIstView的用法,在听了孙老师的课程后,终于对Adapter有了一定的理解,特作此文记之. ListView在App当中应用相当广泛,比如QQ好友列表.微博等等,都是某种特定的列表,所 ...