[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之手势滑动返回功能-b
iOS中如果不自定义UINavigationBar,通过手势向右滑是可以实现返回的,这时左边的标题文字提示的是上一个ViewController的标题,如果需要把文字改为简约风格,例如弄过箭头返回啥的 ...
- Creating Shazam in Java
A couple of days ago I encountered this article: How Shazam Works This got me interested in how a pr ...
- hdu 1824
也是一道2-sat的入门题: 不过题目描述的不清楚,看了别人的题解才知道题意: 和上面的那题差不多,一个模板: 代码: #include<cstdio> #include<stack ...
- Gartner 如何看 RASP 和 WAF?
在这个计算机网络飞速发展的网络时代里,新兴的网络威胁正在不断「侵蚀」着的应用程序和核心数据的安全,各种繁杂的防护手段也随之接踵而来.众所周知,Gartner 是全球最具权威的 IT 研究与顾问咨询公司 ...
- 两台CISCO2691测试静态路由汇总,浮动静态路由和负载分担静态路由配置
Dynagen的配置文件内容: #Lab - autostart = False [localhost] [[]] ram = image = C:\Program Files (x86)\Dynam ...
- 禁止form表单回车键进行提交
使用EasyUI的时候,我们会给一个datagrid添加一个搜索栏,但是这个搜索栏的form,我们一般使用ajax向服务器提交数据,因此在这样一个环境下可以考虑禁止用户按回车键(Enter)提交.代码 ...
- http实现发送post请求,获取结果集
package com.ming; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.Ou ...
- android Service开机启动及debug
开机启动一个service需要做的工作如下: 1.开发一个receiver用于接收系统广播: public class BootReceiver extends BroadcastReceiver { ...
- Ehcache详细解读(转)
Ehcache 是现在最流行的纯Java开源缓存框架,配置简单.结构清晰.功能强大,最初知道它,是从Hibernate的缓存开始的.网上中文的EhCache材料 以简单介绍和配置方法居多,如果你有这方 ...
- 【UE】
1.链接颜色.评论颜色.时间颜色 区分 2.昵称 - 评论 - 时间 用户视线很自然