为什么我们需要使用 flatMap? [英] Why do we need to use flatMap?

查看:26
本文介绍了为什么我们需要使用 flatMap?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用 RxJS,但我不明白为什么在这个例子中我们需要使用像 flatMapconcatAll 这样的函数;这里的数组数组在哪里?

I am starting to use RxJS and I don't understand why in this example we need to use a function like flatMap or concatAll; where is the array of arrays here?

var requestStream = Rx.Observable.just('https://api.github.com/users');

var responseMetastream = requestStream
  .flatMap(function(requestUrl) {
    return Rx.Observable.fromPromise(jQuery.getJSON(requestUrl));
  });

responseMetastream.subscribe(url => {console.log(url)})

如果有人可以直观地解释正在发生的事情,那将非常有帮助.

If someone can visually explain what is happening, it will be very helpful.

推荐答案

当我开始查看 Rxjs 时,我也偶然发现了那块石头.对我有帮助的是以下内容:

When I started to have a look at Rxjs I also stumbled on that stone. What helped me is the following:

  • 来自reactivex.io 的文档.例如,对于 flatMap:http://reactivex.io/documentation/运营商/flatmap.html
  • 来自 rxmarbles 的文档:http://rxmarbles.com/.你不会在那里找到flatMap,你必须查看mergeMap(另一个名字).
  • 你遗漏的 Rx 介绍:https://gist.github.com/staltz/868e7e9bc2a7b8c1f754.它解决了一个非常相似的例子.特别是它解决了这样一个事实,即承诺类似于仅发出一个值的 observable.
  • 最后看看来自 RxJava 的类型信息.未输入 Javascript 在这里没有帮助.基本上,如果 Observable 表示一个推送类型为 T 的值的 observable 对象,那么 flatMap 接受一个 T' -> 类型的函数.Observable 作为其参数,并返回 Observable.map 接受一个 T' -> 类型的函数.T 并返回 Observable.

  • documentation from reactivex.io . For instance, for flatMap: http://reactivex.io/documentation/operators/flatmap.html
  • documentation from rxmarbles : http://rxmarbles.com/. You will not find flatMap there, you must look at mergeMap instead (another name).
  • the introduction to Rx that you have been missing: https://gist.github.com/staltz/868e7e9bc2a7b8c1f754. It addresses a very similar example. In particular it addresses the fact that a promise is akin to an observable emitting only one value.
  • finally looking at the type information from RxJava. Javascript not being typed does not help here. Basically if Observable<T> denotes an observable object which pushes values of type T, then flatMap takes a function of type T' -> Observable<T> as its argument, and returns Observable<T>. map takes a function of type T' -> T and returns Observable<T>.

回到您的示例,您有一个从 url 字符串生成 promise 的函数.所以 T' : stringT : promise.而根据我们之前所说的 promise : Observable,所以 T : Observable,加上 T'' : html.如果你把那个承诺生成函数放在 map 中,你会得到 Observable> 当你想要的是 Observable:您希望 observable 发出 html 值.flatMap 被这样调用是因为它扁平化(移除了一个可观察层)来自 map 的结果.根据您的背景,这对您来说可能是中文,但是通过输入信息和从这里绘制的图,一切都变得清晰起来:http://reactivex.io/documentation/operators/flatmap.html.

Going back to your example, you have a function which produces promises from an url string. So T' : string, and T : promise. And from what we said before promise : Observable<T''>, so T : Observable<T''>, with T'' : html. If you put that promise producing function in map, you get Observable<Observable<T''>> when what you want is Observable<T''>: you want the observable to emit the html values. flatMap is called like that because it flattens (removes an observable layer) the result from map. Depending on your background, this might be chinese to you, but everything became crystal clear to me with typing info and the drawing from here: http://reactivex.io/documentation/operators/flatmap.html.

这篇关于为什么我们需要使用 flatMap?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆