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. designated initializer和secondary initializer是什么?

    仅在此简单记录概念,方便以后回顾... ===================================== designated initializer是指定初始化方法,提供所有参数: sec ...

  2. UML类图的6中关系

    引用自: http://blog.csdn.net/tianhai110/article/details/6339565 UML类图分为如下四种关系: 1.  泛化 (Generalization)  ...

  3. webserver<2>

    #include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <sys/wai ...

  4. [刷机教程] 三星Note8 N5100不卡屏的唯一解决办法--落雨刷机教程

    首先我自己写了一个word,在附件里.大概23页,图文并茂.附带三星NOTE8 N5100 MD2下载包 刷机要谨慎啊,小伙伴们. 刷机教程已经上传到我本人的网站:点击进入去看吧.和word一样. h ...

  5. 【技术贴】解决 myeclipse打不开报错an error has occurred, see .

    方法1.右键选中快捷方式属性选项,在快捷方式页,目标一项最后加上-clean选项,如C:\MyEclipse6\eclipse.exe -clean. 然后重新启动一下MyEclipse. 方法2. ...

  6. 练习生产者与消费者-PYTHON多线程中的条件变量同步-Queue

    以前练习过,但好久不用,手生,概念也生了, 重温一下.. URL: http://www.cnblogs.com/holbrook/tag/%E5%A4%9A%E7%BA%BF%E7%A8%8B/ ~ ...

  7. OUTLOOK连EXCHANGE,配置POP3时跳出错误问题

    "Authentication failed because outlook doesnt support any Resolved Question: "Authenticati ...

  8. 【BZOJ 2154】Crash的数字表格 (莫比乌斯+分块)

    2154: Crash的数字表格 Description 今天的数学课上,Crash小朋友学习了最小公倍数(Least Common Multiple).对于两个正整数a和b,LCM(a, b)表示能 ...

  9. sqlite的源代码加密,以及其它一些文章

    一.       给数据库加密 前面所说的内容网上已经有很多资料,虽然比较零散,但是花点时间也还是可以找到的.现在要说的这个——数据库加密,资料就很难找.也可能是我操作水平不够,找不到对应资料.但不管 ...

  10. Xamarin IOS – hello word

    原文:Xamarin IOS – hello word 环境 硬件:Macbook pro Retina 13 系统:10.11.3 EI Capitan Xcode:7.0 Ps:配置刚刚够用. 安 ...