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. SSM框架+Plupload实现断点续传(Spring+SpringMVC+MyBatis+Plupload)

    关于Plupload的介绍,相信它的官网http://www.plupload.com/已经给得很详细了.Plupload的上传原理简单点说,就是将用户选中的文件(可多个)分隔成一个个小块,依次向服务 ...

  2. IOS中获取文件路径的方法

    iphone沙箱模型的有四个文件夹,分别是什么,永久数据存储一般放在什么位置,得到模拟器的路径的简单方式是什么. documents,tmp,app,Library. (NSHomeDirectory ...

  3. SQL 各种连接:内连接,外连接(左外,右外,完全外)

    在讲述之前,假设有如下两个表EMP, DEPT, 并且他们数据如下:

  4. Servlet高级应用---Servlet与缓存

    一]设置缓存文件的有效日期        重点方法:            HttpServletRequest类:                    1>String getRequest ...

  5. 广州麒麟网络工作室 qlgame eninge(anroid) opengles c++ matrix

    在opengles中,采用的是可编程渲染管线,矩阵需要自己实现! 先说一下矩阵的理论: 参考一下资料:http://blog.sina.com.cn/s/blog_6084f588010192ug.h ...

  6. Android java程序获取assets资产文件

    AssetManager assetManager=this.getAssets(); inputStream = assetManager.open("test.xml");

  7. Web API路由与动作(三)

    本章包括三个小节  如果你输入了mvc的路由规则 这个可以粗略过一遍即可  内容说明有点繁琐 原文地址:http://www.asp.net/web-api/overview/web-api-rout ...

  8. linux VM命令下查找

    使用vi编辑器编辑长文件时,常常是头昏眼花,也找不到需要更改的内容. 这时,使用查找功能尤为重要. 方法如下: 1.命令模式下输入“/字符串”,例如“/Section 3”. 2.如果查找下一个,按“ ...

  9. div居中鼠标悬浮显示下拉列表

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. index 辨别字符在字符串中的位置

    namespace index{    class Program    {        static void Main(string[] args)        {            wh ...