angularrxjspipesubscribefork-join

Angular subscription method with fork-join doesn't work


This my permission service. I wanna get a boolean array.

import { forkJoin, Observable } from "rxjs";
import { map } from 'rxjs/operators';                                                   
           

private hasPermission(lessorId: number, userId: number, permission: string): boolean {
 // returns boolean. It works correctly
}

hasPermissionsForX(permissions: string[]): Observable<boolean[]> {
return forkJoin([
  this.userService.getUser(), //returns Observable
  this.lessorService.getLessor() //returns Observable
]).pipe(
  map(([user, lessor]) => permissions.map(permission => this.hasPermission(lessor.lessorId, user.userId, permission)))
)}

However, my subscription method is not working. The method had not been getting step into the function. I can't even get console.log response. This is my subscription method.

const permissionsToCheck = ['firstPermission', 'secondPermission', 'thirdPermission'];
this.permissionService.hasPermissionsForX(permissionsToCheck).subscribe(([firstPermission, secondPermission, thirdPermission]) => {
  console.log(firstPermission, secondPermission, thirdPermission)                              
})

What am I supposed to do? What am I doing wrong?

Thanks in advance!


Solution

  • Judging by the name of your service methods, I'd assume that you actually wanted to use zip which emits after all Observables emit, instead of forkJoin which will only emit once all source observables complete.

    Also, keep in mind that every observable needs a subscriber in order to emit anything. So, make sure that you actually subscribe to the result of hasPermissionsForX().

    zip

    forkJoin