[rxjs] Creating An Observable with RxJS
Create an observable
var Observable = Rx.Observable;
var source = Observable.create(function(observe){
var person = {
name: "Zhentian",
message: "Hello World!"
};
observe.onNext(person);
observe.onCompleted();
});
var sub = source.subscribe(function onNext(person){
console.log(person.name + ' say ' + person.message);
}, function onError(err){
console.log(err);
}, function onCompleted(){
console.log("done");
});
//Zhentian say Hello World!
//done
Async
var Observable = Rx.Observable;
var source = Observable.create(function(observe){
setTimeout(function(){
var person = {
name: "Zhentian",
message: "Hello World!"
};
observe.onNext(person);
observe.onCompleted();
}, 1000);
console.log("ansyc finished!");
});
var sub = source.subscribe(function onNext(person){
console.log(person.name + ' say ' + person.message);
}, function onError(err){
console.log(err);
}, function onCompleted(){
console.log("done");
});
//"ansyc finished!"
//"Zhentian say Hello World!"
//"done"
Dispose the async
When you dispose the operation, we can see it log out "start timeout", which is not good, because, the onNext() would never be called, what we want is it even don't get inside setTimeout function.
var Observable = Rx.Observable;
var source = Observable.create(function(observe){
setTimeout(function(){
console.log("Starat timeout");
var person = {
name: "Zhentian",
message: "Hello World!"
};
observe.onNext(person);
observe.onCompleted();
}, 1000);
console.log("ansyc finished!");
});
var sub = source.subscribe(function onNext(person){
console.log(person.name + ' say ' + person.message);
}, function onError(err){
console.log(err);
}, function onCompleted(){
console.log("done");
});
setTimeout(function(){
sub.dispose();
}, 500);
/*
"ansyc finished!"
"Starat timeout"
*/
Define the dispose
We can give setTimeout and id, and in the return function, we clear this timeout.
var Observable = Rx.Observable;
var source = Observable.create(function(observe){
var id = setTimeout(function(){
console.log("Starat timeout");
var person = {
name: "Zhentian",
message: "Hello World!"
};
observe.onNext(person);
observe.onCompleted();
}, 1000);
console.log("ansyc finished!");
// Note that this is optional, you do not have to return this if you require no cleanup
return function(){
clearTimeout(id);
}
});
var sub = source.subscribe(function onNext(person){
console.log(person.name + ' say ' + person.message);
}, function onError(err){
console.log(err);
}, function onCompleted(){
console.log("done");
});
setTimeout(function(){
sub.dispose();
}, 500);
/*
"ansyc finished!"
*/
Catch error
If we throw an error in the code, but we found it actually not catched by the onError handler.
var Observable = Rx.Observable;
var source = Observable.create(function(observe){
var id = setTimeout(function(){
throw "there is an error"; //Throw an error here
var person = {
name: "Zhentian",
message: "Hello World!"
};
observe.onNext(person);
observe.onCompleted();
}, 1000);
// Note that this is optional, you do not have to return this if you require no cleanup
return function(){
clearTimeout(id);
}
});
var sub = source.subscribe(function onNext(person){
console.log(person.name + ' say ' + person.message);
}, function onError(err){
console.log("Error: " + err);
}, function onCompleted(){
console.log("done");
});
/*
"error"
"Uncaught there is an error (line 6)"
*/
What we can do is to add try catch in the block.
var Observable = Rx.Observable;
var source = Observable.create(function(observe){
var id = setTimeout(function(){
try{
throw "there is an error"; //Throw an error here
var person = {
name: "Zhentian",
message: "Hello World!"
};
observe.onNext(person);
observe.onCompleted();
}catch(error){
observe.onError(error);
}
}, 1000);
// Note that this is optional, you do not have to return this if you require no cleanup
return function(){
clearTimeout(id);
}
});
var sub = source.subscribe(function onNext(person){
console.log(person.name + ' say ' + person.message);
}, function onError(err){
console.log("Error: " + err);
}, function onCompleted(){
console.log("done");
});
/*
"Error: there is an error"
*/
[rxjs] Creating An Observable with RxJS的更多相关文章
- Angular快速学习笔记(4) -- Observable与RxJS
介绍RxJS前,先介绍Observable 可观察对象(Observable) 可观察对象支持在应用中的发布者和订阅者之间传递消息. 可观察对象可以发送多个任意类型的值 -- 字面量.消息.事件. 基 ...
- rxjs简单的Observable用例
import React from 'react'; import { Observable } from 'rxjs'; const FlowPage = () => { const onSu ...
- [Angular] Creating an Observable Store with Rx
The API for the store is really simple: /* set(name: string, state: any); select<T>(name: stri ...
- [RxJS] Creating Observable From Scratch
Get a better understanding of the RxJS Observable by implementing one that's similar from the ground ...
- [Javascript + rxjs] Introducing the Observable
In this lesson we will get introduced to the Observable type. An Observable is a collection that arr ...
- 九、Rxjs请求对Observable进行封装
1.引入 Http.Jsonp.Rxjs 三个模块 2.请求中添加一个 .map(res => res.json) 问题 1.Property 'map' does not exist on t ...
- [RxJS] Subject: an Observable and Observer hybrid
This lesson teaches you how a Subject is simply a hybrid of Observable and Observer which can act as ...
- rxjs——subject和Observable的区别
原创文章,转载请注明出处 理解 observable的每个订阅者之间,是独立的,完整的享受observable流动下来的数据的. subject的订阅者之间,是共享一个留下来的数据的 举例 这里的cl ...
- [RxJS + AngularJS] Sync Requests with RxJS and Angular
When you implement a search bar, the user can make several different queries in a row. With a Promis ...
随机推荐
- iOS分类中通过runtime添加动态属性
这个的话并不是说 可以 在程序运行的时候 来几个 未知的东西 就添加什么 1 2 3 4 5的属性.而是可以在系统原有类的基础上 给那个类 集合实际的工程来添加你方便实用的东西.比如 ...
- CSS禅意
标题取自<css禅意花园>一书,还记得当年读此书时的情景,真的是内容和书名一样的优秀,就以此标题作为自己在该文的一种追求吧,尽管我的水平和见解都和Dave Shea相去甚远.该文算是对前两 ...
- 一篇文章让你彻底搞清楚Python中self的含义
刚开始学习Python的类写法的时候觉得很是麻烦,为什么定义时需要而调用时又不需要,为什么不能内部简化从而减少我们敲击键盘的次数? 你看完这篇文章后就会明白所有的疑问. self代表类的实例,而非类. ...
- lua方法点(.)调用和冒号(:)调用区别:
用.定义方法时object.func_name(arg1,arg2...),方法真正的函数签名形式为: object.func_name(arg1, arg2...) 用:定义方法时object:fu ...
- jquery仿ios日期时间插件
Demo下载: 手机时间控件.zip 使用之前,请在页面中加入以下js和css: jquery-1.9.1.js mobiscroll.core-2.5.2.js mobiscroll.core-2. ...
- Python零散收集:
Python零散收集 转义字符 描述 \(在行尾时) 续行符 \\ 反斜杠符号 \’ 单引号 \” 双引号 \a 响铃 \b 退格(Backspace) \e 转义 \000 空 \n 换行 \v 纵 ...
- IIS tomcat共用80端口解决一个IP多个域名:使用Nginx反向代理方式使两者兼容
环境: windows server 2003,IIS6服务器,Tomcat7服务器 域名有几个: 以下是使用IIS的域名: http://www.formuch.com/ http://www.fo ...
- ANDROID_MARS学习笔记_S02_012_ANIMATION_利用AnimationListener在动画结束时删除或添加组件
一.代码 1.xml(1)activity_main.xml <?xml version="1.0" encoding="utf-8"?> < ...
- vs2010自带的报表
本例用来显示Northwind中的order details表中的数据交分组 1.建立一WinForm程序,并建立一数据库连接,选择order details表,此时会自动建立一个xsd的数据集类,如 ...
- android Theme使用三
☆ obtainStyledAttributes参数说明 和使用说明 1) obtainStyledAttributes(int[]attrs) int[] attrs返回的是attrs.xml里一 ...