I have a component that makes use of a service, inside the service is a HTTP call to a server that contains data regarding release notes for a product. The release notes are simply returned as a JSON string.
I want to be able to obtain the JSON value from the server and then return the value from the service to the parent class that called the method in the first place. This way I am able to use the JSON string in the parent class.
I'm not sure what the best way to go about this is. Should I be using a BehaviourSubject with a getter that the parent class can access? If so how do I adapt the below code to make use of a behaviour subject?
Service method
getLatestReleaseNote() {
let releaseNoteUrl = this._configService.getConfig().releaseNoteUrl;
const currentVersion = this.getCurrentVersion();
releaseNoteUrl += currentVersion.replace(".", "_") + ".json";
const releaseNoteDataStream = this._httpClient.get<string>(releaseNoteUrl).pipe(take(1)); //Need to subscribe or pass to BehaviourSubject
}
Alternatively, can I just subscribe to the releaseNoteDataStream
observable and return the value from inside the subscription? Like below?
getLatestReleaseNote(): string {
let releaseNoteUrl = this._configService.getConfig().releaseNoteUrl;
const currentVersion = this.getCurrentVersion();
releaseNoteUrl += currentVersion.replace(".", "_") + ".json";
const releaseNoteDataStream = this._httpClient.get<string>(releaseNoteUrl).pipe(take(1)).subscribe(val =>
{
return val;
});
}
I realise this is incorrect but I'm not sure how I go about returning from a subscription.
For a third alternative, could I use an EventEmitter to emit the string value when the subscription has a value?