2

I have a pipe on a observable, the code is like below:

.pipe(
concatMap(() => this.security.getUser()),
tap((partyId) => {
    if (!partyId) {
            window.location.assign(`${environment.redirectURL1}/dashboard/login`);
    }
}),
concatMap((partyId) => this.security.getType(partyId)),
tap(async (result) => {
    if (result) {
            this.loggingService.setUser(result.partyId, result.partyType, result.lendingType);
    }else{
            this.loggingService.setUser(partyId, 'null', 'null'); //In here how can I get partyId??
    }
})
)

As you can see, In the last tap the result I am getting have partyId, because I am concatenating partyId which I am sending to the previous concatMap method which is concatMap((partyId) => this.security.getType(partyId)), however, I also want partyId if the result is null as well, so my question is how can I access the partyId in the last concatMap which I am getting from first concatMap which is concatMap(() => this.security.getUser())?

1
  • Pipe and then map your getUser() service call and return the combined result to the tap after that - something like return {result, partyId}. (Also, the async in the tap is useless) Commented Sep 5, 2022 at 16:37

1 Answer 1

2

You can add another .pipe() after your other call, so you end up with nested pipes:

.pipe(
  concatMap(() => this.security.getUser()),
  tap(partyId => {
    if (!partyId) {
       window.location.assign(`${environment.redirectURL1}/dashboard/login`);
    }
  }),
  concatMap(partyId => this.security.getType(partyId).pipe(
    tap(result => {
      if (result) {
        this.loggingService.setUser(result.partyId, result.partyType, result.lendingType);
      } else {
            this.loggingService.setUser(partyId, 'null', 'null');
      }
    })
  ))
);

This technique is explained in more detail in this answer.

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

Comments

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.