Home > Blockchain >  How to call multiple apis call one after another
How to call multiple apis call one after another

Time:09-29

I am learning RxJS. I have 3 api call's, I need to make 2nd api call, and pass it's data as a parameter to 3rd api call. I tried this:

     checkPermission(permissionName: string): Observable<boolean> {
    this.check(this.p1)
      .pipe(
        switchMap(res => {
          const shouldCheck = res.Value;
          if (shouldCheck.toLowerCase() === 'true') {
            return this.checkPermission(permissionName).pipe(
              map(result => {
               
                return result;
              })
            );
          } else return of(true);
        })
      )
      .subscribe(permission => {
       
      });
   
  }

But getting syntax error.

CodePudding user response:

The code you posted is not very comprehensible, but I'll try to make something usable out of it:

  checkPermission(permissionName: string): Observable<boolean> {
    return this.checkSetting(this.p1).pipe(
      map((res) => res.SysConfig.Value.toLowerCase() === 'true'),
      switchMap((shouldCheck) =>
        iif(
          () => shouldCheck,
          this.permission(this.p2).pipe(
            switchMap((data) =>
              this.hasPermission(permissionName, data.SysConfig.Value)
            ),
            // hoping that res.permission is a boolean
            map((res) => res.permission)
          ),
          of(true)
        )
      )
    );
  }

You should also omit the subscribe if you plan on returning Observable<boolean>. You can subscribe to the observable returned by this method in the place where you call it:

this.authService.checkPermission(permission).subscribe(hasPermission => {
  console.log(`User ${hasPermission ? 'has' : 'does not have'} ${permission} permission`);
});

CodePudding user response:

You should use switchMap operator in proper order i.e.

.pipe(
    switchMap(() => {
        return this.someFunctionThatReturnsObservable();
    }),
    // here you will have access to data you've returned in previous (in data variable)
    switchMap((data) => {
        return this.someOtherRequest(data.param1, data.param2).pipe(
            map(result => {
              return result.permission;
            })
        );
    })
)

Remember that you need to place rxjs operators one after another in pipe separating them with coma. Problem is that you have incorrect syntax trying to put some code without operators inside pipe, these lines are problem here:

return this.permission(this.p2).pipe(
    const shouldEncrypt = res.SysConfig.Value;

It is not fully clear what exactly you want to do here, but I assume that you want to conditionally return request or empty observable, you need to add operator before placing above code.

CodePudding user response:

Try it, I have not tested though.

   checkPermission(permissionName: string): Observable<boolean> {
      return this.checkCompanySettingForPermission(this.p1)
          .pipe(
            switchMap(res => this.permissionAPI(res, permissionName)),
            tap(permission => this.$permissionSub.next(permission)),
            switchMap(_=> this.$permissionSub.asObservable())
          )
     }
    
    permissionAPI(res :any, permissionName: string): Observable<any>{
        const shouldCheck = res.SysConfig.Value;
        if (shouldCheck.toLowerCase() === 'true') {
           return this.permissionAPI2(permissionName);
        }
        return of(true);
    }

  permissionAPI2(permissionName: string) :Observable<boolean>{
    return this.checkCompanySettingForPermission(this.p2).pipe(
       switchMap(data => 
        this.hasPermission(permissionName, data.SysConfig.Value).pipe(
              map(( {permission})=>permission)
            )
        )
     );
  }
  • Related