We will learn how to perform network requests to a backend using RxJS Observables.

A example of basic jquery request:

console.clear();
var requestStream = Rx.Observable.just('https://api.github.com/users'); //Current requestStream is just a stream
//We need to subscribe it to make it work
requestStream.subscribe(url => { //Preform a serve reqest by jQuery
jQuery.getJSON(url).done( res => {
console.log(res);
})
});

But it not make so many sence we use jQuery to handle the promise since we already using RxJS:

console.clear();
var requestStream = Rx.Observable.just('https://api.github.com/users'); //Current requestStream is just a stream
//We need to subscribe it to make it work
requestStream.subscribe( url => { //Using Rx.Observable.fromPromise() to handle the response
//Since jQuery.getJSON(url) return a promise
//there we put into the fromPromise() function
var responseStream = Rx.Observable.fromPromise(jQuery.getJSON(url)); //Then subscribe the responseStream
responseStream.subscribe( res => {
console.log(res);
});
});

We see that we can accomplish with promise we also can do in Observable. And the main problem for promise is that promise only even yield a single value. But observalbe can have mult events.

But soon we find we subscribe an stream inside another subscribe, this is what we don't have to do, normal way to avoid this is using flatMap().

Here we do flagMap() but not map() is because inside map() a observable and return another observable then we got an observable of observable.

console.clear();
var requestStream = Rx.Observable.just('https://api.github.com/users');
var responseStream = requestStream
.flatMap( url => Rx.Observable.fromPromise(jQuery.getJSON(url))); responseStream.subscribe( res => console.log(res));

Now we have only one subscribe.

[Reactive Programming] Async requests and responses in RxJS的更多相关文章

  1. [RxJS] Reactive Programming - New requests from refresh clicks -- merge()

    Now we want each time we click refresh button, we will get new group of users. So we need to get the ...

  2. [RxJS] Reactive Programming - Clear data while loading with RxJS startWith()

    In currently implemention, there is one problem, when the page load and click refresh button, the us ...

  3. [RxJS] Reactive Programming - Rendering on the DOM with RxJS

    <!DOCTYPE html> <html> <head> <script src="https://code.jquery.com/jquery- ...

  4. [RxJS] Reactive Programming - Using cached network data with RxJS -- withLatestFrom()

    So now we want to replace one user when we click the 'x' button. To do that, we want: 1. Get the cac ...

  5. [Reactive Programming] RxJS dynamic behavior

    This lesson helps you think in Reactive programming by explaining why it is a beneficial paradigm fo ...

  6. [RxJS] Reactive Programming - What is RxJS?

    First thing need to understand is, Reactive programming is dealing with the event stream. Event stre ...

  7. .Net中的反应式编程(Reactive Programming)

    系列主题:基于消息的软件架构模型演变 一.反应式编程(Reactive Programming) 1.什么是反应式编程:反应式编程(Reactive programming)简称Rx,他是一个使用LI ...

  8. ReactiveCocoa与Functional Reactive Programming

    转自 http://blog.leezhong.com/ios/2013/06/19/frp-reactivecocoa.html Functional Reactive Programming(以下 ...

  9. "Principles of Reactive Programming" 之 <Persistent Actor State>学习笔记

    这是<Pinciples of Reactive Programming>week6的最后一课. 为什么需要把actor的状态持久化? 如果actor没有状态,那么在任何实时,这个acto ...

随机推荐

  1. Ext.Date 方法

    1.Ext.Date.add(date,interval,value); 提供执行基本日期运算的简便方法; date 日期对象, interval 一个有效的日期间隔枚举值, value 向当前日期上 ...

  2. 面试cookie

    cookie : 存储数据,当用户访问了某个网站(网页)的时候,我们就可以通过cookie来像访问者电脑上存储数据 1.不同的浏览器存放的cookie位置不一样,也是不能通用的 2.cookie的存储 ...

  3. Contest20140710 loop bellman-ford求负环&&0/1分数规划

    loop|loop.in|loop.out 题目描述: 给出一个有向带权图,权为边权,求一个简单回路,使其平均边权最小. 简单回路指不多次经过同一个点的回路. 输入格式: 第一行两个整数,表示图的点数 ...

  4. BZOJ 3992 序列统计

    Description 小C有一个集合\(S\),里面的元素都是小于\(M\)的非负整数.他用程序编写了一个数列生成器,可以生成一个长度为\(N\)的数列,数列中的每个数都属于集合\(S\). 小C用 ...

  5. 11个显著提升 ASP.NET 应用程序性能的技巧——第1部分

    [编者按]本文出自站外作者 Brij Bhushan Mishra ,Brij 是微软 MVP-ASP.NET/IIS.C# Corner MVP.CodeProject Insider,前 Code ...

  6. ibatis 中isNull, isNotNull与isEmpty, isNotEmpty区别

    在iBATIS中isNull用于判断参数是否为Null,isNotNull相反 isEmpty判断参数是否为Null或者空,满足其中一个条件则其true isNotEmpty相反,当参数既不为Null ...

  7. Spring REST

    前面介绍过Spring的MVC结合不同的view显示不同的数据,如:结合json的 view显示json.结合xml的view显示xml文档.那么这些数据除了在WebBrowser中用JavaScri ...

  8. 居然还有WM_TIMECHANGE(只在用户手动改变系统时间时才会产生作用)

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  9. Spring MVC 解读——@RequestMapping (1)(转)

    转自:http://my.oschina.net/HeliosFly/blog/212329 Spring MVC 解读——@RequestMapping 为了降低文章篇幅,使得文章更目标化,简洁化, ...

  10. Mysql 8个小时连接断开问题解析

    wait_timeout — 指的是mysql在关闭一个非交互的连接之前所要等待的秒数,其取值范围为1-2147483(Windows),1-31536000(linux),默认值28800. int ...