0

I have a setup on the backend where I fetch users info from one endpoint and their profile photos from another one.

I need to first fetch users and then their photos before displaying anything on the screen.

I've got a following interface

interface User {
  id: string;
  name: string;
  email: string;
  photoUrl?: string;
}

and two functions that return observables: getUsers and getUserPhoto.

I make the call like so:

this.getUsers().pipe(
  map((users: User[]) => {
    return users.forEach(user => {
      // this part is problematic as I don't know how to call it properly
      return this.getUserPhoto(user.id)
    })
  })
);

Ideally, the end result of this call should be a User array with all properties including photoUrl.

2
  • use pipe on map and inside map operator, use Array.map(). Commented Nov 4, 2019 at 10:16
  • can you please show an example? It's unclear to me Commented Nov 4, 2019 at 10:19

2 Answers 2

2

Try this


this.getUsers.pipe(
    switchMap(users => {
        return forkJoin(users.map(user => this.getPhoto(user.userPhotoUrl).pipe(map(photo => ({...user, photo})))))
})
)

you will have an array of users with photo property

Sign up to request clarification or add additional context in comments.

Comments

0

You can use concat operator from rxjs to chain your api calls, follow this link for more details : https://www.learnrxjs.io/operators/combination/concat.html

1 Comment

I don't think it works in my case. Because the second http call result should update the results of the first http call, and not just concatenate results of two http calls.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.