AsyncSubject emit the last value of a sequence only if the sequence completed. This value is then cached forever, and any other Observer that subscribes after the value has been emmitted will receive it right away.

AsyncSubject is convenient for asynchronous operations that return a single value, such as Ajax requests.

var subject = new Rx.AsyncSubject();
var delayedRange = Rx.Observable.range(,).delay(); delayedRange.subscribe(subject); //----[0,1,2,3,4]|
// AsyncSubject
//----4| subject.subscribe(
function onNext(item) { console.log('Value:', item); },
function onError(err) { console.log('Error:', err); },
function onCompleted() { console.log('Completed.'); }
) /*
"Value:"
4
"Completed."
*/

A more useful example:

function getProducts(url){
var subject; return Rx.Observable.create(function(observer){
//If first time, then create AsyncSubject
if(!subject){
subject = new Rx.AsyncSubject();
//Subscribe to subject
Rx.DOM.get(url).subscribe(subject);
}
//If subject already exists, then just subscribe observer
return subject.subscribe(observer);
})
} var products = getProducts('/products'); // Will trigger request and receive the response when read
products.subscribe(
function onNext(result) {console.log('Result 1: ', result.response)},
function onError(error) {console.log('ERROR', error)}
); // Will receive the result immediately because it is cached
setTimeout(function(){
products.subscribe(
function onNext(result) {console.log('Result 2: ', result.response)},
function onError(error) {console.log('ERROR', error)}
)
}, 5000)

[RxJS] AsyncSubject的更多相关文章

  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 and ReplaySubject - Learn the Differences

    We can use Subject as Observable and Observer: // Subject should be only used to emit value for priv ...

  3. RxJS之AsyncSubject

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

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

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

  5. [译]RxJS 5.X基础篇

    欢迎指错与讨论 : ) 当前RxJS版本:5.0.0-beta.10.更详细的内容尽在RxJS官网http://reactivex.io/rxjs/manual/overview.html.文章比较长 ...

  6. RxJS v6 学习指南

    为什么要使用 RxJS RxJS 是一套处理异步编程的 API,那么我将从异步讲起. 前端编程中的异步有:事件(event).AJAX.动画(animation).定时器(timer). 异步常见的问 ...

  7. RxJS - Subject(转)

    Observer Pattern 观察者模式定义 观察者模式又叫发布订阅模式(Publish/Subscribe),它定义了一种一对多的关系,让多个观察者对象同时监听某一个主题对象,这个主题对象的状态 ...

  8. RxJS库

    介绍 RxJS是一个异步编程的库,同时它通过observable序列来实现基于事件的编程.它提供了一个核心的类型:Observable,几个辅助类型(Observer,Schedulers,Subje ...

  9. rxjs的世界

    rxjs学习了几个月了,看了大量的东西,在理解Observable的本文借鉴的是渔夫的故事,原文,知识的主线以<深入浅出rxjs>为主,动图借鉴了rxjs中文社区翻译的文章和国外的一个动图 ...

随机推荐

  1. C#中父窗口和子窗口之间实现控件互操作

    很多人都苦恼于如何在子窗体中操作主窗体上的控件,或者在主窗体中操作子窗体上的控件.相比较而言,后面稍微简单一些,只要在主窗体中创建子窗体的时候,保留所创建子窗体对象即可. 下面重点介绍前一种,目前常见 ...

  2. Java [leetcode 38]Count and Say

    题目描述: The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, ...

  3. Java [leetcode 17]Letter Combinations of a Phone Number

    题目描述: Given a digit string, return all possible letter combinations that the number could represent. ...

  4. 在.net中用Connection对象数据源的架构信息

    可得到数据库中的,表,视图,等信息   string strcon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + fi ...

  5. C# 网页图片爬虫的几种技术基础

    一.文件流方式获取网络图片资源 方法1 , ); System.Net.WebRequest webreq = System.Net.WebRequest.Create(url); System.Ne ...

  6. android广告平台介绍

    广告模式:   广告条:最普遍的广告模式,嵌入在应用界面内,用户点击行为会带来收入.   积分墙:应用通过限制功能.去广告等引导用户进入积分墙页面下载广告应用得到积分来换取使用的模式,用户安装完推荐广 ...

  7. HDU 4003-Find Metal Mineral(树状背包)

    题意: n个节点的树给出每个边的权值,有k个机器人,求由机器人走完所有节点的最小花费(所有机器人开始在根节点) 分析: 仔细看了几遍例题后,发现这个题的状态很巧妙,先从整体考虑,一个机器人走完所有边回 ...

  8. AJAX— 异步传输

    AJAX异步传输 AJAX— 异步传输 AJAX浏览器与服务器异步传输数据,通过异步请求大大减少数据信息访问量,通俗:AJAX对局部刷新而不是对整个页面刷新,AJAX使得程序更快,更友好. AJAX处 ...

  9. 你真的知道HTML吗?

    经过几次面试当中,被问及到最基础的东西,没想到回答不上来,有点蛋痛,今天特地的复习了一下!! 内容: 1.Doctype(文档类型)的作用是什么?有多少文档类型? 2.浏览器标准模式和怪异模式之间的区 ...

  10. 5.1 CUDA atomic原子操作

    和许多多线程并行问题一样,CUDA也存在互斥访问的问题,即当一个线程改变变量X,而另外一个线程在读取变量X的值,执行原子操作类似于有一个自旋锁,只有等X的变量在改变完成之后,才能执行读操作,这样可以保 ...