Reputation: 1038
I have a scenario with 3 webservices, say A, B and C.
A and B can be invoked without arguments. C requires parameters deriving from both A and B. For example:
http.get("A").subscribe(x => console.log(x))
// prints 'goofy'
http.get("B").subscribe(y => console.log(y))
// prints 'mickey'
http.get("C?param1=goofy¶m2=mickey").subscribe(z => console.log(z))
// prints 'daisy'
I wonder how to create a single Observable that does the job, and whose subscription return the result from webservice C.
obs1 = http.get("A")
obs2 = http.get("B")
f3 = (x,y => http.get("C?param1="+x+"¶m2="+y))
bigObservable = someconcatenationof(obs1, obs2, f3)
and I would like that
bigObservable.subscribe(z => console.log(z))
// should print "daisy"
I suppose that this question can be a little bit generalized: A, B, C do not need to be webservices, they can just be some kind of Observable's, where C's constructor requires results from both A and B.
Upvotes: 0
Views: 48
Reputation: 11934
I think this would be one approach:
forkJoin({
a: http.get("A"),
b: http.get("B"),
}).pipe(
mergeMap(({ a, b }) => http.get(`C?param1=${a}¶m2=${b}`))
)
forkJoin
will subscribe to all the provided observables at the same time and will emit their last values when all of them complete.
Upvotes: 4