Recently, I centralized duplicate code into a common service that efficiently handles retrieving a user's permissions. The idea is that upon instantiation of the service, it contacts our identity server to obtain the user's permissions and caches them for future use. This way, subsequent permission checks from components utilize the cached data instead of bombarding the identity server and causing delays in the user experience.
I am currently loading permissions within the service's onInit method, keeping track of whether the process is complete using a specific flag.
To implement this functionality, I attempted something like the following:
private isDataLoaded: boolean = false;
public userHasPermission(permissionName: string): boolean{
while(!this.isDataLoaded)
{
// Implement logic to synchronously wait/sleep for a second before rechecking
}
return userPermissions[permissionName] == true;
}
However, I'm struggling to find a way to make the thread sleep synchronously. Most solutions I've come across involve asynchronous methods which use promises when the operation completes...which doesn't align with the requirements of this scenario. How can I achieve synchronous sleep