[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 ...
随机推荐
- ORA-01033 ORA-01109 ORA-01034 ORA-12514 ORA-24324 ORA-01041 ORA-01157 ORA-01110
客户数据库挂掉了 在plsql客户端使用普通账号登录时提示如下错误 因为好久没弄数据库了,慌了一小下. 接下来搜索过往的知识,回忆.在cli下输入了以下命令 sqlplus system/system ...
- ios:如何将自己编写的软件放到真正的iPhone上运行(转)
想要将自己编写的软件放到真正的iPhone上去运行,首先你需要成为Apple Developer计划的成员.其次,你需要设置程序ID和认证书,在这之后你就可以在你指定的iPhone上运行你的程序了.下 ...
- 使用ImageMagick和Tesseract进行简单数字图像识别
使用ImageMagick和Tesseract进行简单数字图像识别 由于直接使用 tesseract 进行识别,识别率很低, ImageMagick 安装.配置及使用: 平台:winXP 1. 安装I ...
- Hbase案例分析(二)
情景1:
- CKplayer 新手入门超简单使用教程
网页播放器都有使用的前提(问1). ~~~~~~~分隔线~~~~~~~ 只需一步先看播放器效果(问2): 下载附件,解压内容(ckplayer文件夹和ckplayer.html)到网站根目录,在浏览器 ...
- Table嵌套去掉子table的外边框
Table表格去掉子表格的边框 1. 父表格 <table align="center" style="border:none;cell-padding:0; ce ...
- XSS与字符编码的那些事儿
目录 0x00:基本介绍 0x01:html实体编码 0x02:新增的实体编码 实体编码变异以及浏览器的某些工作原理! 0x03:javascript编码 0x04:base64编码 0x05:闲扯 ...
- webview调用javascript脚本无反应
最近遇到一个问题:在html中有一段javascript脚本定义了一个方法,在使用webview.loadUrl("javascript:方法名()")时方法未执行,后来 查资料发 ...
- ASP.NET MVC 增强Convert用法+【分页2】
[分页2] public dynamic PageQuery() { : : ; i < data.Length; i++) ...
- jQuery开始之旅
(1)首先到http://jquery.com/download/网站进行现在jQuery库. (2)这里演示一下,使用CDN进行jQuery编程的例子: 选择的是jQuery的CDN,<scr ...