问题描述
我正在阅读Angular 2教程,我的内容是关于路由器的部分,特别是关于使用Observable从URL提取参数的部分.它为此使用了Observables.
I am going through the Angular 2 tutorial and I'm on the part about routers, specifically the part about using an Observable to extract the parameters from a URL. It uses Observables for this.
部分代码是这样的:
ngOnInit(): void {
this.route.params
.switchMap((params: Params) => this.heroService.getHero(+params['id']))
.subscribe(hero => this.hero = hero);
}
和 getHero()
方法:
getHero(id: number): Promise<Hero> {
return this.getHeroes()
.then(heroes => heroes.find(hero => hero.id === id));
}
如您所见, getHero()
的类型很强,以返回Hero类型的Promise.它返回 then()
调用的结果,据我所知,这是一个Promise.
As you can see, getHero()
is strongly typed to return a Promise of type Hero. It returns the result of a then()
call, which, so far as I understand, is a Promise.
所以 switchMap()
收到了Promise的回信.据我了解 switchMap()
的功能,它会使用给出的所有内容,并从中创建一个新的Observable.然后 subscribe()
获取Observable持有的值,并将其分配给 this.hero
.
So switchMap()
receives a Promise back. As I understand the functionality of switchMap()
, it takes whatever it is given and makes a new Observable out of it. subscribe()
then takes the value that Observable holds and assigns it to this.hero
.
这是我不明白的: hero
实例变量的类型很强,可以容纳 Hero
对象.由于 switchMap()
从 getHero()
的返回值中创建了一个Observable,而 getHero()
返回了Promise,在我看来,则该 subscribe()
正在将 Promise< Hero>
分配给 this.hero
.我不明白Promise是如何解决的,它的实际值是如何提取出来并放入 hero
实例var中的.
Here is what I don't get: The hero
instance variable is strongly typed to hold a Hero
object. Since switchMap()
creates an Observable out of getHero()
's return value, and getHero()
returns a Promise, it seems to me, then, that subscribe()
is assigning a Promise<Hero>
to this.hero
. I don't understand how that Promise is getting resolved and its actual value is getting extracted and put into the hero
instance var.
感谢任何对此有见识的人!
Thanks to anyone who can shed any insight on this!!
推荐答案
这是RxJS 5的未充分记录的功能之一.所有希望从某些回调工作中接收到Observable的运算符实际上都是与Observables,Promises,迭代器,数组,...
That's one of not very well documented features of RxJS 5. All operators that expect to receive an Observable from some callback work in fact with Observables, Promises, iterators, arrays, ...
看看这些:
- http://reactivex.io/rxjs/class/es6/MiscJSDoc.js~ObservableInputDoc.html
- http://reactivex.io/rxjs/class/es6/MiscJSDoc.js~SubscribableOrPromiseDoc.html
因此,发生什么情况是 switchMap()
订阅了从 getHero(...)
返回的 Promise
,就像它是一个可观察的对象一样
So what happens is that switchMap()
subscribes to the Promise
returned from getHero(...)
just like it was an Observable.
源代码的核心部分在这里:
The core part of the source code is here:
在 subscribeToResult
中- https://github.com/ReactiveX/rxjs/blob/master/src/util/subscribeToResult.ts#L42
此原理的一个非常普遍的用法是将一系列物品拆开成单独的排放物时.例如:
A very common usage of this principle is when unpacking an array of items into separate emissions. For example:
Observable.of([1,2,3,4])
.concatAll()
.subscribe(console.log);
这将打印:
1
2
3
4
观看现场演示: https://jsbin.com/sevale/4/edit?js,控制台
即使根据手册 concatAll()
只能与发出Observable的Observable正式使用.
It works even though according to the manual concatAll()
works officially only with Observables emitting Observables.
这篇关于switchMap()如何解决Promise?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!