I am writing a service function where is it supposed to reply the latest price of a product. I connect to my server - using websocket and I get a DataStream using the Rxjs observable, now the function in the service layer is supposed to reply back only the value and not the Observable object.
Sample code I am trying
public getRealTimePrice(id: string): number {
let ws = new $WebSocket('/price?Id=' + id + '&connectionId=' + this.randomUUID());
this.priceWebsocket = ws;
this.priceWebsocket.getDataStream()
.map(msg => JSON.parse(msg.data).price)
.subscribe(responseNumber => {
return responseNumber // by the time execution reaches here the context is changed.
});
// I am not returning anything on the function level - which results in a compilation error as per typescript.
}
but for obviously reason I am unable to return the number - since it is inside the subscribe callback where the context changes and the function is async
How can I await for the reply from Observable and then return only the value received in the observable.