3

So because of the various setups we have I have a habit of running into places where I'd like to add the result of one observable to another, and then using both. Where I need the first one to complete before the other one

getUser()
  .pipe(
    mergeMap(user => forkJoin([
      of(user),
      getSomethingWithUser(user)
    ]))
  )
  .subscribe((result: [User, SomethingWithUser]) => /*And then use them*/)

The various *Map functions just seem to return the result of the final observable, which doesn't work so well when I want the result of them all

Ja Da
  • 230
  • 2
  • 12
  • What specifically is wrong with your current solution? I'm confident that it should work. – Lukas-T Nov 25 '22 at 13:34
  • @churill I'm not sure if it's wrong. But it does feel kinda off to return an observable with the value of another observable – Ja Da Nov 28 '22 at 11:49
  • I'd say it's correct, but a bit clumsy. BizzyBob's answer looks good :) – Lukas-T Nov 28 '22 at 12:47

1 Answers1

4

You can simply do a map after the second call and add a nested pipe to access both the first value and second value:

getUser().pipe(
    mergeMap(user => getSomethingWithUser(user).pipe(
        map(something => [user, something])
    )
    .subscribe(([user, something]) => /*And then use them*/)

This answer has some more details on using the nested pipe.

BizzyBob
  • 12,309
  • 4
  • 27
  • 51