Wait for singleton service result before continuing - Angular
Solution 1:
I think you can get away with the call of this.User()
in the service's constructor.
Just define a variable userDetails
in your class as you already have and have your User()
method like:
public User():Observable<IEmployee> {
if(this.userDetails === undefined){
return this.service.get('/some/url')
.do((resp: IResponse) => {
this.userDetails = resp.Response
})
.map((resp: IResponse) => {
return resp.Response
});
}
else {
return Observable.of(this.userDetails)
}
}
Whenever any of the components will subscribe, a call will go to the endpoint if userDetails
doesn't already exist.