[Reactive Programming] Async requests and responses in RxJS
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的更多相关文章
- [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 ...
- [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 ...
- [RxJS] Reactive Programming - Rendering on the DOM with RxJS
<!DOCTYPE html> <html> <head> <script src="https://code.jquery.com/jquery- ...
- [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 ...
- [Reactive Programming] RxJS dynamic behavior
This lesson helps you think in Reactive programming by explaining why it is a beneficial paradigm fo ...
- [RxJS] Reactive Programming - What is RxJS?
First thing need to understand is, Reactive programming is dealing with the event stream. Event stre ...
- .Net中的反应式编程(Reactive Programming)
系列主题:基于消息的软件架构模型演变 一.反应式编程(Reactive Programming) 1.什么是反应式编程:反应式编程(Reactive programming)简称Rx,他是一个使用LI ...
- ReactiveCocoa与Functional Reactive Programming
转自 http://blog.leezhong.com/ios/2013/06/19/frp-reactivecocoa.html Functional Reactive Programming(以下 ...
- "Principles of Reactive Programming" 之 <Persistent Actor State>学习笔记
这是<Pinciples of Reactive Programming>week6的最后一课. 为什么需要把actor的状态持久化? 如果actor没有状态,那么在任何实时,这个acto ...
随机推荐
- javascript 获取 class 样式 重新赋值class样式 为div等系列标签内更改内容
name = document.getElementById(project_not_through_id).className; // 获取目标id的 cla ...
- jQuery 侧栏菜单点击body消失
其实就在弹出菜单时 让菜单外部有个全屏大小的遮罩层
- cx_Oracle使用方法一
cx_Oracle使用方法 正确安装好cx_oracle之后,要使用它来连接到oracle数据库进行操作,具体应该分3步走: 第一步:导入cx_Oracle ,建立连接 >>> im ...
- 《python源码剖析》笔记一——python编译
1.python的架构: 2.python源码的组织结构: 3.windows环境下编译python:
- www.nygwkt.com
南京宁阳制冷设备维修有限公司是专业从事厨房空调,http://www.nygwkt.com岗位空调制冷设备设计.制造.安装.改造.维修.保养的专业化公司.在南京享有很高的客户评论. 我们对南京宁阳制冷 ...
- [BZOJ 1907] 树的路径覆盖 【树形DP】
题目链接:BZOJ - 1907 题目分析 使用树形 DP,f[x][0] 表示以 x 为根的子树不能与 x 的父亲连接的最小路径数(即 x 是一个折线的拐点). f[x][1] 表示以 x 为根的子 ...
- android的原理,为什么不需要手动关闭程序
转自android的原理,为什么不需要手动关闭程序 不用在意剩余内存的大小,其实很多人都是把使用其他系统的习惯带过来来了. Andoird大多应用没有退出的设计其实是有道理的,这和系统对进程的调度机制 ...
- servlet和struts2一起使用,实现绝对路径下的图片输出到jsp页面
如果我们在web.xml中配置的struts2的接收请求的路径为: <filter-mapping> <filter-name>struts2</filter-name& ...
- ServletContextListener作用(转)
ServletContext 被 Servlet 程序用来与 Web 容器通信.例如写日志,转发请求.每一个 Web 应用程序含有一个Context,被Web应用内的各个程序共享.因为Context可 ...
- 【HDOJ】3367 Pseudoforest
并查集. #include <cstdio> #include <cstring> #include <cstdlib> #define MAXN 10005 #d ...