Home > Back-end >  How to use another subscribe when the parameters passed to second subscribe depends on first in Angu
How to use another subscribe when the parameters passed to second subscribe depends on first in Angu

Time:07-18

I have first subscribe called like this

this.Service.createUser(details).subscribe(x => {
      let param = {
        'paramUserId': x.userId,
      }
     
    }, err => {
    }); 

Here x.userId i get from first subscribe now i have to call second subscribe and pass this x.userId to second subscribe should i call this inside first subscribe or i call the second subscribe outside the first then suppose if first subscribe method not got completed second one will be called because parameter of first depends on the calling of the first subscribe.

Any Solution Thanks

CodePudding user response:

You can use pipe and switchMap.

this.Service.createUser(details)
    .pipe(switchMap((x: any) => getAnotherObservable(x)))
    .subscribe();

switchMap subscribes to another observable and returns the result: https://www.learnrxjs.io/learn-rxjs/operators/transformation/switchmap

CodePudding user response:

You can achieve this by many ways. Here is just one:

this.Service.createUser(details).pipe(
  .tap(d => this.Service.secondSubscriberService(d))
)
  .subscribe(x => {
    let param = { paramUserId: x.userId };
  });
  • Related