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的更多相关文章

  1. Angular快速学习笔记(4) -- Observable与RxJS

    介绍RxJS前,先介绍Observable 可观察对象(Observable) 可观察对象支持在应用中的发布者和订阅者之间传递消息. 可观察对象可以发送多个任意类型的值 -- 字面量.消息.事件. 基 ...

  2. rxjs简单的Observable用例

    import React from 'react'; import { Observable } from 'rxjs'; const FlowPage = () => { const onSu ...

  3. [Angular] Creating an Observable Store with Rx

    The API for the store is really simple: /* set(name: string, state: any); select<T>(name: stri ...

  4. [RxJS] Creating Observable From Scratch

    Get a better understanding of the RxJS Observable by implementing one that's similar from the ground ...

  5. [Javascript + rxjs] Introducing the Observable

    In this lesson we will get introduced to the Observable type. An Observable is a collection that arr ...

  6. 九、Rxjs请求对Observable进行封装

    1.引入 Http.Jsonp.Rxjs 三个模块 2.请求中添加一个 .map(res => res.json) 问题 1.Property 'map' does not exist on t ...

  7. [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 ...

  8. rxjs——subject和Observable的区别

    原创文章,转载请注明出处 理解 observable的每个订阅者之间,是独立的,完整的享受observable流动下来的数据的. subject的订阅者之间,是共享一个留下来的数据的 举例 这里的cl ...

  9. [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 ...

随机推荐

  1. iOS打电话

    1,这种方法,拨打完电话回不到原来的应用,会停留在通讯录里,而且是直接拨打,不弹出提示NSMutableString * str=[[NSMutableString alloc] initWithFo ...

  2. 移动web HTML5使用photoswipe模仿微信朋友圈图片放大浏览

    先来几张效果图: 点击其中一张照片可放大,可支持图片文字描述: 同时支持分享功能: 支持手势放大缩小 使用js框架是PhotoSwipe. PhotoSwipe是一个图片放大插件,兼容pc和移动端,经 ...

  3. bzoj 1023: [SHOI2008]cactus仙人掌图 tarjan缩环&&环上单调队列

    1023: [SHOI2008]cactus仙人掌图 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 1141  Solved: 435[Submit][ ...

  4. IAR中 C语言位定义

     __IO_REG8_BIT( SYS,             0xFFFFF802, __READ_WRITE ) #define __IO_REG8_BIT(NAME, ADDRESS, A ...

  5. ORA-32001: write to SPFILE requested but no SPFILE specified at startup

    SQL> alter system set  sga_max_size=2048M scope=spfile; alter system set  sga_max_size=2048M scop ...

  6. VirtualBox设置共享文件夹和镜像访问的方法

    VirtualBox设置共享文件夹和镜像访问的方法 virtualBox是一款虚拟机软件,可以在该软件上安装各类的操作系统,至于如何安装请参见另外一篇经验<如何使用VirtualBox安装win ...

  7. Ehcache详细解读(转)

    Ehcache 是现在最流行的纯Java开源缓存框架,配置简单.结构清晰.功能强大,最初知道它,是从Hibernate的缓存开始的.网上中文的EhCache材料 以简单介绍和配置方法居多,如果你有这方 ...

  8. Oracle core06_latch&lock

    lock and latch 在oracle中为了保护共享资源,使用了两种不同的锁机制lock和latch,这两种锁有明显不同点: 1,lock和pin,采用的是队列的方式,先来先服务的策略,latc ...

  9. ssh 登录出现的几种错误以及解决办法

    首先.确保server端的ssh服务是开的(service shhd start) 然后在client端输入: ssh usrname@serverip (远程登录) scp filename usr ...

  10. C# partial 局部类型

    关键字partial是一个上下文关键字,只有和 class.struct.interface 放在一起时才有关键字的含义.因此partial的引入不会影响现有代码中名称为partial的变量.局部类型 ...