Saturday, May 11, 2024
 Popular · Latest · Hot · Upcoming
29
rated 0 times [  31] [ 2]  / answers: 1 / hits: 16607  / 9 Years ago, wed, december 2, 2015, 12:00:00

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.



I figured out to use 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
}
);


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.



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...


More From » promise

 Answers
0

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 */
})


In short, forkJoin has semantics similar to that of RSVP.all if you know the promises library RSVP.


[#64182] Tuesday, December 1, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
harleyaleenag

Total Points: 678
Total Questions: 121
Total Answers: 105

Location: Papua New Guinea
Member since Thu, Jul 9, 2020
4 Years ago
harleyaleenag questions
Thu, May 5, 22, 00:00, 2 Years ago
Wed, Aug 19, 20, 00:00, 4 Years ago
;