Home > database >  I need to join the two requests in one, how can I do it in angular/rxjs?
I need to join the two requests in one, how can I do it in angular/rxjs?

Time:06-14

This is my code in which I am using forkJoin to make the two requests.

  private getUserInfo() {
    this.spinner.show();
    let res1 = this.user.getUser()
    let res2 = this.authService.getAccountMe();
   forkJoin([res1, res2]).subscribe(([data1, data2]) => {
    this.spinner.hide();
    

CodePudding user response:

Right now you are getting the data and not doing anything with it. Instead, the function should return an observable that returns the data, right?

I think what you're looking for is this...

private getUserInfo() {
  this.spinner.show();
  const res1 = this.user.getUser();
  const res2 = this.authService.getAccountMe();
  return forkJoin([res1, res2]).pipe(
    tap(() => this.spinner.hide()),
    shareReplay(1) // not needed if only one thing will be using the returned observable.
  );
}

CodePudding user response:

if you want an unique object you can use pipe(map) in the way

forkJoin([res1, res2]).pipe(
    map(([res1, res2]: [any, any]) => ({ ...res1, ...res2 }))
  );

or

forkJoin([res1, res2]).pipe(
    map(([res1, res2]: [any, any]) => ({ user:res1, account:res2 }))
  );

But it's a bit confused your question

  • Related