In my typescript code, I have a function that looks like this:
getConfigurations() {
let sessionConfig = sessionStorage.getItem('config');
if(sessionConfig)
return of(sessionConfig);
else {
this.dataService.getRoute('configurations').subscribe(
route => {
this.http.get<any[]>(route.ToString()).subscribe(
result => {
sessionStorage.setItem('config', result);
return of(result);
},
error => {
alert(error);
}
)
},
error => {
alert(error);
}
);
}
}
This function is supposed to either return a string value if the 'config' key in sessionStorage already exists or retrieve the value from the backend using dataService and save it in the session storage. However, I am having trouble returning the value stored in sessionStorage (result) as well.
The getRoute function from dataService looks like this:
getRoute(service: string){
return this.http.get('urlToMyBackendWebApi');
}
Here, http refers to the Angular HttpClient.
I am wondering how I can access the value returned by getConfigurations(). I tried subscribing to getConfigurations(), and retrieving the value from the if condition works fine, but I'm unsure about how to get the result from the else condition. Should I return an observable for the entire block? If so, how would I go about reading the return value?