So far, when writing these subscribe functions, we haven't returned anything. It is possible return an unsubscribe function from a subscribe function. In this lesson we will see how to allow observers to subscribe and unsubscribe from an Observable.

var foo = new Rx.Observable(function subscribe(observer) {
var id = setInterval(function () {
observer.next('hi');
}, );
return function unsubscribe() {
clearInterval(id);
};
}); var subscription = foo.subscribe({
next: function (x) { console.log('next ' + x); },
error: function (err) { console.log('error ' + err); },
complete: function () { console.log('done'); },
}); setTimeout(function () {
subscription.unsubscribe();
}, );

[RxJS] Returning subscriptions from the subscribe function的更多相关文章

  1. [Go] Returning Multiple Values from a Function in Go

    Returning multiple values from a function is a common idiom in Go, most often used for returning val ...

  2. [RxJS] Basic DOM Rendering with Subscribe

    While frameworks like Angular 2 and CycleJS provides great ways to update the DOM and handle subscri ...

  3. MVVM架构~knockoutjs系列之从Knockout.Validation.js源码中学习它的用法

    返回目录 说在前 有时,我们在使用一个插件时,在网上即找不到它的相关API,这时,我们会很抓狂的,与其抓狂,还不如踏下心来,分析一下它的源码,事实上,对于JS这种开发语言来说,它开发的插件的使用方法都 ...

  4. [TypeScript] Understanding Generics with RxJS

    Libraries such as RxJS use generics heavily in their definition files to describe how types flow thr ...

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

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

  6. RxJS库

    介绍 RxJS是一个异步编程的库,同时它通过observable序列来实现基于事件的编程.它提供了一个核心的类型:Observable,几个辅助类型(Observer,Schedulers,Subje ...

  7. rxjs的世界

    rxjs学习了几个月了,看了大量的东西,在理解Observable的本文借鉴的是渔夫的故事,原文,知识的主线以<深入浅出rxjs>为主,动图借鉴了rxjs中文社区翻译的文章和国外的一个动图 ...

  8. [RxJS] Error handling operator: catch

    Most of the common RxJS operators are about transformation, combination or filtering, but this lesso ...

  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. asp.net中Page.ClientScript.RegisterStartupScript用法小结(转)

    //ASP.NET后台页面跳转 Page.ClientScript.RegisterStartupScript(Page.GetType(), "", "<scri ...

  2. Xml解析之——Java/Android/Python

    Xml解析之——Java/Android/Python 一.Xml文件 test.xml <note> <to>George</to> <from>Jo ...

  3. SQLServer2008收缩数据库日志

    -- Set to SIMPLE mode ALTER DATABASE [DATABASE_NAME] SET RECOVERY SIMPLE; -- Shrink the db ); -- Set ...

  4. GCD多线程 在子线程中获取网络图片 在主线程更新

    子线程中得所有数据都可以直接拿到主线程中使用 //当触摸屏幕的时候,从网络上下载一张图片到控制器的view上显示 -(void)touchesBegan:(NSSet *)touches withEv ...

  5. JavaScript--格式化当前时间

    <!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. Javascript模块化编程 require.js使用详解

    一.为什么用require.js,产生的背景 最早的时候,所有Javascript代码都写在一个文件里面,只要加载这一个文件就够了.后来,代码越来越多,一个文件不够了,必须分成多个文件,依次加载. & ...

  7. python种的builtin函数详解-第三篇

    exec_stmt ::= "exec" or_expr ["in" expression ["," expression]] eval(e ...

  8. PHP之路——PHPExcel使用

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGMAAAJkCAIAAAA6GnvRAAAgAElEQVR4nOzd918bV/ov8Pv33Y2RNC

  9. 使用cglib动态创建java类

    转至:http://ckwang17.iteye.com/blog/963881 cglib 是一个开源项目! 是一个强大的,高性能,高质量的Code生成类库,它可以在运行期扩展Java类与实现Jav ...

  10. C++访问权限【原创】

    可以访问private的有:1.本类的函数:2.友元函数.   可以访问protected的有:1.本类的函数:2.子类函数:3.友元函数.   可以访问public的有:1.本类的函数和对象:2.子 ...