What's the difference between map and flatmap? First, let's show what map is. To show that, I need a source stream, so I'm going to make an interval. It takes a tenth of a second, and I'm only going to take 10 values, and subscribe to it.

var source = Rx.Observable.interval(100).take(10).map((x) => x*2);
console.clear();
source.subscribe(function(res){
console.log(res);
}); /*
0
2
4
6
8
10
12
14
16
18
*/

But what happens if I want to do something asynchronous in here? To show this, I'm going to return an observable timer, which is just going to wait for half of a second, and then map to exactly the same values.

var source =
Rx.Observable.interval(100).take(10)
.map(function(x){
return Rx.Observable.timer(100).map(function(){
return x;
});
});
console.clear();
source.subscribe(function(res){
console.log(res.toString());
}); /*
"[object Object]"
"[object Object]"
"[object Object]"
"[object Object]"
"[object Object]"
"[object Object]"
"[object Object]"
"[object Object]"
"[object Object]"
"[object Object]"
*/

'Rx.Observable.timer(100)' causes it delay 100 ms.

You find You're going to get objects back. That's because these objects are observables. I'd really, really like to get my values back into my stream. There's actually two ways to do this.

First Way: mergerAll()

What a merge all does is, it takes in a stream of observables, or an observable of observables, and merges them together as they come in.

It subscribes to each one, and pumps them into one output stream.

var source =
Rx.Observable.interval(100).take(10)
.map(function(x){
return Rx.Observable.timer(100).map(function(){
return x;
});
}).mergeAll();
console.clear();
source.subscribe(function(res){
console.log(res.toString());
}); /*
"0"
"1"
"2"
"3"
"4"
"5"
"6"
"7"
"8"
"9"
*/

Second way: flatMap()

What flatmap is going to do is it's going to perform this mapping function and then subscribe to each observable returned by the map.

var source =
Rx.Observable.interval(100).take(10)
.flatMap(function(x){
return Rx.Observable.timer(100).map(function(){
return x;
});
});
console.clear();
source.subscribe(function(res){
console.log(res.toString());
});

[RxJS] map vs flatMap的更多相关文章

  1. 第35讲:List的map、flatMap、foreach、filter操作代码实战

    List类的高阶方法          val fmap = List( 1,2 ,3). map { _ + 1 } //List(2, 3, 4)    val fruit_rev2 = frui ...

  2. scala学习笔记(8): 列表的map,flatMap,zip和reduce

    map,flatMap,zip和reduce函数可以让我们更容易处理列表函数. 1 map函数map将一个函数应用于列表的每一个元素并且将其作为一个新的列表返回.我们可以这样对列表的元素进行平方: s ...

  3. java8中stream的map和flatmap的理解

    转自https://blog.csdn.net/wynjauu/article/details/78741093 假如我们有这样一个需求给定单词列表["Hello","W ...

  4. map和flatmap的区别+理解、学习与使用 Java 中的 Optional

    转自:map和flatmap的区别 对于stream,   两者的输入都是stream的每一个元素,map的输出对应一个元素,必然是一个元素(null也是要返回),flatmap是0或者多个元素(为n ...

  5. spark中map与flatMap的区别

    作为spark初学者对,一直对map与flatMap两个函数比较难以理解,这几天看了和写了不少例子,终于把它们搞清楚了 两者的区别主要在于action后得到的值 例子: import org.apac ...

  6. Spark中map与flatMap

    map将函数作用到数据集的每一个元素上,生成一个新的分布式的数据集(RDD)返回 map函数的源码: def map(self, f, preservesPartitioning=False): &q ...

  7. 第88讲:Scala中使用For表达式实现map、flatMap、filter

    今天我们来学习一下如何使用for表达式实现map.flatMap以及filter 首先,我们来看下map.map的功能是,传入一个list,通过一个函数f,将list中的元素A变成元素B的过程.最后得 ...

  8. Swift中的map 和 flatMap 原理及用法

    之前对这两个概念有点糊,今天正好遇到一个相关需求,才深入了解了下. 需求如下: 大概就是对一个数组的model,重构成一个新model,返回得到一个新数组 用map很容易实现,不过后来我需要对其中进行 ...

  9. 【Android】RxJava的使用(三)转换——map、flatMap

    前两篇Android RxJava的使用(一)基本用法.Android RxJava的使用(二)Action介绍了RxJava的基本用法,对Rxjava还不了解的请先看以上两篇.这篇为大家讲解RxJa ...

随机推荐

  1. 使用Node.js作为后台进行爬虫

    看了一遍又一遍Node.js但是没过多久就又忘了,总想找点东西来练练手,就发现B站首页搜索框旁边的GIF图特别有意思,想着是不是可以写一个小Node.js项目把这些图全部扒下来,于是带着复习.预习与探 ...

  2. jQuery开发技术笔记

    HTML DOM 加载步骤    1. 解析 HTML 结构     2.加载外部脚本和样式表文件     3.解析并执行脚本代码     4.构造 HTML DOM 模型     5.加载图片等外部 ...

  3. 从一个标准 url 里取出文件的扩展名

    在php预定义函数中有一个叫做"pathinfo()"的函数,专门用于返回文件路径信息的. 那好,我们就来看一下它能为我们做些什么?       语法:pathinfo($url_ ...

  4. ThinkPHP Uploadify 图片上载

    从官方网站下载的Uploadify最新版本:http://www.uploadify.com/download/ jQuery库是1.7.1版本. 下载好的Uploadify目录下面的文件有: 用到的 ...

  5. 自定义函数中的参数返回值 “-> (Int -> Int)”的问题

    func makeIncrementer() -> (Int -> Int) { func addOne(number: Int) -> Int { + number } retur ...

  6. max os 安装python模块PIL

    下载libjpeg和zlib: http://www.ijg.org/files/jpegsrc.v9.tar.gz http://zlib.net/zlib-1.2.8.tar.gz 安装libjp ...

  7. iOS:图像和点击事件

    问题:如何区分点的是哪张图片? // // main.m // Hello // // Created by lishujun on 14-8-28. // Copyright (c) 2014年 l ...

  8. jQuery中的data方法:

    向元素附加数据,然后取回该数据: $("#btn1").click(function(){ $("div").data("greeting" ...

  9. 李洪强iOS开发之-环信02_iOS SDK 介绍及导入

    李洪强iOS开发之-环信02_iOS SDK 介绍及导入 iOS SDK 介绍及导入 iOS SDK 介绍 环信 SDK 为用户开发 IM 相关的应用提供的一套完善的开发框架.包括以下几个部分: SD ...

  10. java基础随笔-overload和override

    今天重温了一下方法重载和方法重写. 首先是方法重写(override)的几点要求: 1.必须继承父类或者实现某接口的方法. 2.方法名称和参数必须和父类(或者实现的接口方法)完全一致. 3.重写的修饰 ...