问题描述
我有三个promise,Rest请求返回数据列表。
第三个有前两个列表的引用(ids),所以我想把这些id映射到相应的名字数据。
映射不是问题,我只是使用Lodash。
但问题是在开始计算这个映射之前等待三个promises解决。
I have three promises, Rest requests returning lists of data.
The third one has references (ids) to the first two lists, so I want to map these ids to corresponding names when I have all the data.
The mapping isn't an issue, I just use Lodash for that.
But the issue is to wait for the three promises to resolve before starting to computing this mapping.
我想出去使用 concat()
:
Rx.Observable.concat(p1, p2, p3).subscribe(
function onNext(list)
{
// Assign the list to the corresponding variable in the scope
},
function onError(e)
{
// Notify of error
},
function onCompleted()
{
// Do the mapping
}
);
我的问题是 onNext()
将以随机顺序调用。 IE浏览器。我不知道我在某个时候收到了哪个列表,并且很难从它包含的数据中得知。
My problem is that onNext()
will be called in random order. Ie. I don't know which list I receive at some point, and it is hard to tell from the data it contains.
有没有办法跟踪产生哪些承诺清单?拉链? concatMap? concatMapObserver?我承认我还没有完全理解最后两个的使用...
Is there a way to track which promises produced which list? A kind of zip? concatMap? concatMapObserver? I admit I haven't fully understood the usage of the last two...
推荐答案
如果这些是我们正在谈论的承诺,我想你可以看一下 forkJoin
运算符。参看,
If these are promises we are talking about, I think you can have a look at the forkJoin
operator. Cf. https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/forkjoin.md, http://xgrommx.github.io/rx-book/content/observable/observable_methods/forkjoin.html
你可以得到类似的东西:
You could then have something like :
Rx.Observable.forkJoin(p1, p2, p3, function(p1,p2,p3){
/* your combining code here */
})
简而言之, forkJoin
具有类似于 RSVP.all
的语义,如果您知道promises库RSVP。
In short, forkJoin
has semantics similar to that of RSVP.all
if you know the promises library RSVP.
这篇关于RxJS:结束三个承诺,区分结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!