Is the following method the correct way to return a value?
private canView(permission: string, resource: string): boolean {
let result: boolean;
this.accessChecker.isGranted(permission, resource)
.pipe(
take(1)
)
.subscribe(
granted => {
result = granted;
}
);
return result;
}
This method is used to set the show property for a menu item:
@Component({
selector: 'ngx-pages',
styleUrls: ['pages.component.scss'],
template: `
<ngx-one-column-layout>
<nb-menu [items]="menu"></nb-menu>
<router-outlet></router-outlet>
</ngx-one-column-layout>
`,
})
export class PagesComponent {
menu: NbMenuItem[] = [
{
title: 'Admin area',
icon: 'settings-outline',
show: this.canView('view', 'it_only'),
children: [
{
title: 'config.json',
link: '/pages/admin-it/config-json',
},
{
title: 'JWT',
link: '/pages/admin-it/jwt',
},
]
}]
constructor(private accessChecker: NbAccessChecker) { }
private canView(permission: string, resource: string): boolean {
// see above
}
Is it possible for the result to be returned before its value is set in the subscription (since subscribe is asynchronous)? If so, what is the correct approach to take?