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. leetcode修炼之路——387. First Unique Character in a String

    最近公司搬家了,有两天没写了,今天闲下来了,继续开始算法之路. leetcode的题目如下: Given a string, find the first non-repeating characte ...

  2. c - 计算1到20的阶乘

    #include <stdio.h> /* 题目:求 1+2!+3!+...+20!的和 */ unsigned long long int factorial(long n) { uns ...

  3. JavaScript typeof, null, 和 undefined

    typeof 操作符 你可以使用 typeof 操作符来检测变量的数据类型. 实例 typeof "John"                // 返回 string typeof ...

  4. javascript 操作符类型隐性转换

    javascript 操作符类型隐性转换 (一).一元操作符只能操作一个值的操作符叫做一元操作符1.递增和递减操作符a. 在应用于一个包含有效数字字符的字符串时,先将其转换为数字值,再执行加减1的操作 ...

  5. 微信小应用vs progressive-web-apps

    https://developers.google.com/web/progressive-web-apps/

  6. birt 运行环境搭建(部署到tomcat)

    最近一直在研究eclipse的birt,各种坑~~~~(>_<)~~~~. Requirements:tomcat version:7.0,birt-runtime-4.6.0-20160 ...

  7. MySQL主从复制详细部署过程

    环境介绍:   采用多实例进行主从复制测试,多实例方法请参考网上其它文档,其实多实例和双服务器对于测试环境来说是一样的.   当前采用3306端口进程为Master,3307端口进程为Slave.   ...

  8. Jquery学习笔记1-jquery总体代码框架

    第一次在博客中记录自己的笔记,希望能坚持下去吧,加油! 今天学习的是Jquery的源代码,官网上下载,然后使用DW(dream waver)编辑器打开Js(下载的是未压缩版),版本是2.0.3.第一次 ...

  9. javascript在一个字符串中每隔多少字符插入某个字符串

    function insertStr(str,tar,n,m){ var x='' var str=str.split('') if(str.length==0) return for(var i=n ...

  10. kill tomcat process in window

    1.通过命令netstat -ano | findstr 8080找到tomcat所占用的process,如下图   2.执行ntsd -c q -p 7944 kill刚刚找到的process,然后 ...