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. php脚本业务逻辑

    设置时区 设置执行不超时 设置根目录常量 引入配置文件(自定义/模板) 错误记录 定义业务类 执行业务类::run() 数据库单例初始化(连接) 日志单例初始化(引入日志类,配置日志路径,日志开关) ...

  2. php之面向对象(2)

    注意:看这篇文章之前建议看看之前的文章,因为内容之间衔接性比较强.勿喷.. 面向对象,是一种思维模式的名字,并不是指某种特定的写法,面向对象简称oop,思路的核心在于:什么时候 什么东西 做什么. 编 ...

  3. 那些年被我坑过的Python——邂逅与初识(第一章)

    第一问:为什么学习Python? 虚妖说:为了还债,还技术债,很早接触编程,却一直徘徊,也码了很多代码,却从未真真学会编程! 第二问:什么是Python 是一种以简洁.优雅著称的解释型.动态.强类型的 ...

  4. 简单学C——第六天

    指针    指针是c语言中很灵活的一个内容,当然,灵活的都是较难掌握的.不过,只要理解其实质,学习,运用指针还是一件很轻松的事情的. 首先理解,1.什么是指针? 在c语言中,指针也同Int ,doub ...

  5. void (*f(int, void (*)(int)))(int) 函数解析 转

    今天与几个同学看到了一个函数指针定义: void (*f(int, void (*)(int)))(int) 以前在C trap pit fails里面见过,但是文章里面介绍的很详细,但是往往使初学者 ...

  6. iOS:NAV+TABLE结合

    功能:点击列表项,用列表字符串作为参数创建一个新视图,新视图默认可以有一个BACK按钮回到上一个视图 // // main.m // Hello // // Created by lishujun o ...

  7. LightOj_1284 Lights inside 3D Grid

    题目链接 题意: 给一个X * Y * Z 的立方体, 每个单位立方体内都有一盏灯, 初始状态是灭的, 你每次操作如下: 1)选择一个点(x1, y1, z1)     再选择一个点(x2, y2, ...

  8. 应用Java(环境变量)

    工作中,不一定非要设置Java环境变量 因为,IDE自身环境的设置,代替了系统环境变量 环境变量 系统的环境变量,相当于软件工作的环境.工作中,经常需要设置以下变量: Path ClassPath 自 ...

  9. Spring MVC 中的REST支持

    本部分提供了支持 RESTful web 服务的主要 Spring 功能(或注释)的概述. @Controller 使用 @Controller 注释对将成为 MVC 中控制器的类进行注释并处理 HT ...

  10. mysql grant 示例

    ' with grant option; FLUSH PRIVILEGES;