1

In the below code, the execution never goes to mergemap. Looking for some help with correcting this code.

Requirement is, once i get response from getBns method, i need to perform some operation with response and then using the tap operator to update few flags that are used in HTML.

Component.ts*

 this.bns$ = this.myService.getBns().pipe(
          mergeMap((bns) => {
            performloadKey()
            return bns
          }),
          tap((bns) => {
            console.log(bns);
          })
        );


this.bns$.pipe(takeUntil(this.destroyed$)).subscribe();

service.ts

getBns(): Observable<any>{
    return of(mockData); 
  }
Deadpool_er
  • 215
  • 4
  • 20

2 Answers2

2

I suppose the performLoadKey is also returning an observable? If so, I guess you want to do the following:

this.bns$ = this.myService.getBns().pipe(
  mergeMap((bns) => performloadKey().pipe(
    mapTo(bns)
  )),
  tap((bns) => console.log(bns))
);
Poul Kruijt
  • 69,713
  • 12
  • 145
  • 149
  • Thanks for reply. That's correct it is returning observable but my question is, its not even going into mergeMap to execute the performloadkey irrespective of having pipe and mapTo there. – Deadpool_er Aug 25 '21 at 10:10
  • can you show the `performloadkey` method. My guess is that it returns an `Observable`, which will only fire when you subscribe to it. Just executing it like that inside a `mergeMap` won't do anything, unless you specifically return it. Then it will get subscribed to – Poul Kruijt Aug 25 '21 at 10:13
2

MergeMap only works if you return an Observable inside the callback. You are subscribing of(bns) so inside the callback you are returning the value bns. If bns isn't an Observable and doesn't emit any values, the pipe never continues.

Tobias S.
  • 21,159
  • 4
  • 27
  • 45
  • Thank you. That makes sense. So in this case if i want to execute performLoadkey which returns an observable and then want to use tap to update few flags how should i procedd ? – Deadpool_er Aug 25 '21 at 10:14