I have an app that needs to retrieve some properties from a REST call, and then those properties get used at other places in the code. The problem is that sometimes the code tries to use the properties before they are fully retrieved.
@Injectable()
export class PropertiesService
{
public properties: any = {};
constructor(
public http: HttpClient
)
{
let url = Environment.hostUrl + "properties/";
this.http.get<ApiResponse>(url).subscribe(
(response) =>
{
console.info("Setting Remote Properties", response.message);
this.properties = response.message;
});
}
public getProperty(propertyName: string)
{
return this.properties[propertyName];
}
}
And then in other places in the code it will call to get a property:
console.info(propertiesService.getProperty("propertyName"));
But if that gets called before the properties response gets back, it will return undefined and I only want the users of the properties to use them once they have been set. Generally the properties response doesn’t take very long to come back (just a few milliseconds), so I was wondering if there was a way to make the "getProperty" method wait until the property is no longer undefined before it returns with the value. But I’m not sure if that is good design and if there is a better way to do it I’d like to know about that.
2
Answers
You can’t return a promise in a class constructor in javascript thus you can’t wait for async code to be executed.
You can do something like this instead:
This will set the promise to a class property and we can wait for the promise to resolve before returning the properties.
You could use a variable isLoading like a semaphore to know when the api has successful retrieved the values.
Then you could use a BehaviourSubject to store in memory the results and get them back whenever you need without calling the api again.
For ex:
Then in you code you need to subscribe to get the value
Using observable is the best way to deal with async functions like rest api call.
This is a dangerous observation. You should never rely on async functions speed. Manage them with observables so you can get the values only when you have them.