I am searching for the most efficient method to combine two functions that return Observables.
The initial function is described as:
abstract getProfiles(): Observable<ClientProfile[]>
The other one is:
abstract getUser(userId: number): Observable<User>
My goal is to create a data structure projection like this:
interface Projection {
user: User,
profiles: ClientProfile[]
}
To achieve this, I am looking to optimize the following function:
getUserProfiles() {
this.userService.getProfiles()
.pipe(
flatMap((ps: ClientProfile[]) => {
ps.map(p => this.userService.getUser(p.userId))
// I NEED TO RETURN AN OBSERVABLE HERE:
// {user: u, ...ps}
})
)
.subscribe(_ => doSomething(_))
What is the most effective way to merge a single user with the list of profiles?